2021.02.05學習總結
阿新 • • 發佈:2021-02-10
技術標籤:spring bootjavaspringrestful過濾器
Jersey框架概述:
閱讀官方文件請點選:jsersey。RESTful Web Services in Java即java中的一種restful框架。jersey使用了JAX-RS規範來約束API的開發。既然jersey是基於restful風格的框架,那麼什麼是restful呢,主要有以下幾點:
在rest認為,一切都可以被稱為資源。
每個資源都由uri標識。要訪問這個資源,必須通過對應的uri去訪問。
訪問資源使用POST,GET,PUT,DELETE。POST為新增介面,GET為獲取介面,PUT為修改介面,DELETE為刪除介面。
每次請求都是獨立的。
1.0新增依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
1.1新增配置類
ackage com.example.demosssss.config; import com.example.demosssss.filter.Myfilter; import com.example.demosssss.resource.UserResource; import org.glassfish.jersey.server.ResourceConfig; import org.springframework.stereotype.Component; import javax.ws.rs.ApplicationPath; /** * @Author: csh * @Description: * @Date: 2021/1/1 * @Modified by: */ @Component @ApplicationPath("/jersey") public class JerseyConfig extends ResourceConfig { public JerseyConfig(){ //packages("com.example.demosssss.resource"); register(UserResource.class);//register新增資源類 } }
1.2application.properties配置
#輸出資料json格式化
spring.jackson.default-property-inclusion=non_null
spring.jackson.serialization.indent-output=true
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.parser.allow-missing-values=true
1.3編寫一個資源類
package com.example.demosssss.resource; import org.springframework.stereotype.Component; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; /** * @Author: csh * @Description: * @Date: 2021/1/1 * @Modified by: */ @Component @Path("/users") public class UserResource { @GET @Produces(MediaType.APPLICATION_JSON) public String get(){ return "hello world"; } }
1.4瀏覽器請求
2.0Jersey過濾器使用
2.1新增依賴
<!-- resteasy -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.0.12.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.12.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-netty</artifactId>
<version>3.0.12.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.12.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.0.12.Final</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-spring</artifactId>
<version>3.0.12.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>3.0.12.Final</version>
</dependency>
2.2編寫過濾器
package com.example.demosssss.filter;
import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
/**
* @Author: csh
* @Description:必須使用@Provider才能起作用,如果有多個過濾器可以用@Priority註解輪序。
* @Date: 2021/1/1
* @Modified by:
*/
@Provider
@Priority(1)
public class Myfilter implements ContainerRequestFilter, ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
System.out.println("containerRequestContext ...111");//請求引數,可以獲取唯一標識,譬如會話token
}
@Override
public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
System.out.println("containerResponseContext ...222");
String s = "123";
containerResponseContext.setEntity(s);//響應引數的重新賦值
}
}
2.3註冊過濾器
@Component
@ApplicationPath("/jersey")
public class JerseyConfig extends ResourceConfig {
public JerseyConfig(){
//packages("com.example.study");
register(UserResource.class);
register(Myfilter.class);//一定要註冊哦,不知道該類的請往上看
}
}
2.4效果
3(jersey和springmvc共存)
只需要在啟動類中例項化jersey配置的路徑。
package com.example.demosssss;
import com.example.demosssss.config.JerseyConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.StringUtils;
import java.net.InetAddress;
import java.net.UnknownHostException;
@SpringBootApplication
public class DemosssssApplication {
public static void main(String[] args) throws UnknownHostException {
ConfigurableApplicationContext context = SpringApplication.run(DemosssssApplication.class, args);
ConfigurableEnvironment env = context.getEnvironment();
String ip = InetAddress.getLocalHost().getHostAddress();
String port = env.getProperty("server.port");
String path = env.getProperty("server.servlet.context-path");
if (StringUtils.isEmpty(path)) {
path = "";
}
System.out.println("\n----------------------------------------------------------\n\t" +
"Application is running! Access URLs:\n\t" +
"Local訪問網址: \t\thttp://localhost:" + port + path + "\n\t" +
"External訪問網址: \thttp://" + ip + ":" + port + path + "\n\t" +
"----------------------------------------------------------");
}
@Bean
public ServletRegistrationBean jersetServlet() {
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new ServletContainer(),"/jersey/*");
registrationBean.addInitParameter(
ServletProperties.JAXRS_APPLICATION_CLASS,
JerseyConfig.class.getName());
return registrationBean;
}
}