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;
import org.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");
           
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值