spring与jersey的集成

时间:2022-05-25 19:34:08
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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
  <!-- 开启注解配置 -->  <context:annotation-config />
  <!-- 对指定的包进行组件扫描 -->  <context:component-scan base-package= "com.hpf" />

</ beans>

至此配置就完成了,可以使用提供资源类了:例如:Hello.javapackage 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");
          
     }
}