spring-boot系列——整合jersey

       原创文章,转载请注明

       jersey作为一个实现了标准rest风格的接口,还是比较好用的。当然使用spring-boot的话,还是spring-mvc更好吧,这里说一下如何spring-boot整合jersey、

       一、依赖

       依赖如下:

<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
    
    <dependency>
	    <groupId>org.glassfish.jersey.ext</groupId>
	    <artifactId>jersey-mvc</artifactId>
	    <version>2.22.2</version>
	</dependency>

       除此之外你也可以集成alibaba的json工具,还是比较好用的

<dependency>
		<groupId>com.alibaba</groupId>
  		<artifactId>fastjson</artifactId>
  		<version>1.1.24-SNAPSHOT</version>
	</dependency>

       如果你不用jersey自身的客户端,使用apache的话,依赖包如下

<dependency>
	    <groupId>log4j</groupId>
	    <artifactId>log4j</artifactId>
	</dependency>
	
	<dependency>
	    <groupId>commons-logging</groupId>
	    <artifactId>commons-logging</artifactId>
	    <version>1.2</version>
	</dependency>
	
	<dependency>
	    <groupId>org.apache.httpcomponents</groupId>
	    <artifactId>httpclient</artifactId>
	     <version>4.5.2</version>
	</dependency>
	
	<dependency>
	    <groupId>org.apache.httpcomponents</groupId>
	    <artifactId>httpcore</artifactId>
	    <version>4.4.4</version>
	</dependency>
	
	<dependency>
		<groupId>commons-fileupload</groupId>
  		<artifactId>commons-fileupload</artifactId>
  		<version>1.2.2</version>
	</dependency>

             httpcore从有些maven仓库无法下载,不知原因,可以手动安装到本地仓库

            二、配置

            jersey需要配置一个servlet,指定该jersey对哪些地址进行响应

@Bean
    public ServletRegistrationBean jerseyServlet() {
		ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/rest/*");
		registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());
		return registration;
    }
           另外需要指定jersey的包给spring管理
public class JerseyConfig extends ResourceConfig{
	public JerseyConfig() {
       register(RequestContextFilter.class);
       //配置restful package.
       packages("com.zs.vehicle.rpc");
    }
}
           到此为止,spring就完成了对jersey的整合
       

没有更多推荐了,返回首页