spring與jersey的集成

pom.xml添加依賴
< dependency>
    <groupId > org.glassfish.jersey.containers</ groupId >
    <!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core"  -->
    <artifactId > jersey-container-servlet </artifactId >
    <version > 2.5</ version >
</ dependency>
<!-- Required only when you are using JAX-RS Client -->
       < dependency>
             < groupId> org.glassfish.jersey.core </groupId >
             < artifactId> jersey-client </artifactId >
             < version> 2.5 </version >
       </ dependency>
<!--與spring集成時添加的關鍵依賴-->
       < dependency>
             < groupId> org.glassfish.jersey.ext </groupId >
             < artifactId> jersey-spring3 </artifactId >
             < version> 2.5 </version >
       </ dependency>

       <!-- <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.0.6.RELEASE</version>
            <scope>compile</scope>
      </dependency> -->
      
< dependency>
       < groupId> commons-logging </groupId >
       < artifactId> commons-logging </artifactId >
       < version> 1.1 </version >
       < exclusions>
             < exclusion>
                   < groupId> javax.servlet </groupId >
                   < artifactId> servlet- api</ artifactId>
             </ exclusion>
       </ exclusions>
</ dependency>
       < dependency>
             < groupId> javax.servlet </groupId >
             < artifactId> servlet- api</ artifactId>
             < version> 2.5 </version >
       </ dependency>


web.xml



< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener </ listener-class>
</ listener>
< context-param>
< param-name> contextConfigLocation </param-name >
< param-value> classpath:applicationContext.xml </param-value >
</ context-param>

   <servlet >
        < servlet-name> Jersey REST Service </servlet-name >
        < servlet-class> org.glassfish.jersey.servlet.ServletContainer </servlet-class >
     <!--這裏只是其中一種暴漏資源的方式-->  
< init-param>
            < param-name> javax.ws.rs.Application </param-name >
            < param-value> com.hpf.restfullservice.spring.Myapplication </param-value >
        </ init-param>
        < load-on-startup> 1</ load-on-startup >
    </servlet >
    <servlet-mapping >
        < servlet-name> Jersey REST Service </servlet-name >
        < url-pattern> /rest/* </url-pattern >
    </servlet-mapping >

Myapplication.java

import org.glassfish.jersey.server.ResourceConfig;
importorg.glassfish.jersey.server.spring.scope.RequestContextFilter;

public class Myapplication extends ResourceConfig{
       public Myapplication(){
      register(RequestContextFilter. class );
      register(Hello. class );//註冊需要暴漏的資源
      
      }
}



applicationContext.xml

< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context ="http://www.springframework.org/schema/context"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
>

  <!-- 開啓註解配置 -->
  <context:annotation-config />

  <!-- 對指定的包進行組件掃描 -->
  <context:component-scan base-package= "com.hpf" />


</ beans>

至此配置就完成了,可以使用提供資源類了:例如:
Hello.java
package com.hpf.restfullservice.spring;

import java.net.URLEncoder;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.CommonProperties;
import org.springframework.beans.factory.annotation.Autowired;

@Path("/hello")

public class Hello {
     
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
* 
* @return String that will be returned as a text/plain response.
*/
     @Autowired
     GreetingService greetingService;
     
     @Path("/")
     @GET
     @Consumes(MediaType.TEXT_PLAIN+";charset=UTF-8")
     @Produces(MediaType.TEXT_PLAIN+";charset=UTF-8")
     public String sayHello(String username) throws Exception{
          
          return greetingService.greet("my");
          
     }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章