springboot整合Jersey
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为删除接口。
通过XML/JSON去通信
每次请求都是独立的。
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;
}
}
本文来自博客园,作者:土木转行的人才,转载请注明原文链接