欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jersey2 与 spring4 整合实践【原创】

程序员文章站 2022-04-07 14:33:23
...
前几年用过jeysey,那都是1.x版本的,jersey2有很多不同之处。这次我们上分布式搜索引擎,提供给业务系统的包装接口,我们准备使用restful接口,因此准备把jersey2和spring4集成起来用。以下是集成过程(本文省去spring 环境的搭建)。
1. pom引入依赖包(jersey.version=2.23)
  <dependency>
		  <groupId>org.glassfish.jersey.ext</groupId>
		  <artifactId>jersey-spring3</artifactId>
		  <version>${jersey.version}</version>
		  <exclusions>
		    <exclusion>
		      <groupId>org.springframework</groupId>
		      <artifactId>spring-core</artifactId>
		    </exclusion>			
		    <exclusion>
		      <groupId>org.springframework</groupId>
		      <artifactId>spring-web</artifactId>
		    </exclusion>
		    <exclusion>
		      <groupId>org.springframework</groupId>
		      <artifactId>spring-beans</artifactId>
		    </exclusion>
		  </exclusions>			
		</dependency>
		<dependency>
		  <groupId>org.glassfish.jersey.media</groupId>
		  <artifactId>jersey-media-json-jackson</artifactId>
		  <version>${jersey.version}</version>
		</dependency>
      <!-- spring4 相关包 -->
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.0.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.0.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.1.0.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.0.RELEASE</version>
      </dependency>
			<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.1.0.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.1.0.RELEASE</version>
      </dependency>

2. web.xml
<servlet>
	    <servlet-name>jersey-serlvet</servlet-name>
	    <servlet-class>
	      org.glassfish.jersey.servlet.ServletContainer
	    </servlet-class>
	    <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>cn.gov.zjport.seg.search.jersey.MyApplication</param-value>
        </init-param>	
	    <load-on-startup>1</load-on-startup>
	  </servlet>
	
	  <servlet-mapping>
	    <servlet-name>jersey-serlvet</servlet-name>
	    <url-pattern>/*</url-pattern>
	  </servlet-mapping>

注:如果不用自定义的 MyApplication, spring bean 将无法注入。
3. rest服务
@Service
@Path("/selsearch")
public class SelSearchServiceImpl implements SelSearchService{
	@Resource
	private SelEsWrappedClient selEsWrappedClient;
	
	@GET
	@Path("/{param}")    
	@Produces(MediaType.TEXT_PLAIN)
	public String search(@PathParam("param") String userName) {
		return selEsWrappedClient.getName()+userName;
	}
}


SelSearchServiceImplSelEsWrappedClient 都是spring管理的bean,需要当作resource 在 MyApplication 中注册

4. 注册器
public class MyApplication extends ResourceConfig {

    /**
     * Register JAX-RS application components.
     */
    public MyApplication () {
    	
    	  //自己写的服务
    	  register(SelSearchServiceImpl.class);

    	  //用 Jackson JSON 的提供者来解释 JSON
    	  register(JacksonFeature.class);
    	
    	  //Spring filter 提供了 JAX-RS 和 Spring 请求属性之间的桥梁
    	  register(RequestContextFilter.class);
    }
}


5. 访问 http://localhost:8080/seg-search-webapp/selsearch/xxx 即可