Jersey在spring环境下的实现

来源:互联网 发布:淘宝免单群是怎么运作 编辑:程序博客网 时间:2024/06/03 19:09

目前项目用到Jersey 在这里记录一下

项目采用maven管理

1:pom.xml引得jersey相关jar

 

Java代码  收藏代码
  1.     <dependency>  
  2.             <groupId>com.sun.jersey</groupId>  
  3.             <artifactId>jersey-core</artifactId>  
  4.             <version>1.11</version>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>com.sun.jersey</groupId>  
  8.             <artifactId>jersey-server</artifactId>  
  9.             <version>1.11</version>  
  10.         </dependency>  
  11.         <dependency>  
  12.             <groupId>com.sun.jersey</groupId>  
  13.             <artifactId>jersey-client</artifactId>  
  14.             <version>1.11</version>  
  15.         </dependency>  
  16.   
  17.         <dependency>  
  18.             <groupId>com.sun.jersey</groupId>  
  19.             <artifactId>jersey-json</artifactId>  
  20.             <version>1.10</version>  
  21.         </dependency>  
  22.           <dependency>  
  23.                <groupId>javax.ws.rs</groupId>  
  24.                <artifactId>jsr311-api</artifactId>  
  25.                <version>1.1.1</version>  
  26.           </dependency>  
Java代码  收藏代码
  1.           <dependency>  
  2.                <groupId>asm</groupId>  
  3.              <artifactId>asm</artifactId>  
  4.              <version>3.2</version>  
  5.         </dependency>  
Java代码  收藏代码
  1. <dependency>  
  2.     <groupId>com.sun.jersey.contribs</groupId>  
  3.     <artifactId>jersey-spring</artifactId>  
  4.     <version>1.11</version>  
  5.     <exclusions>  
  6.         <exclusion>  
  7.             <groupId>org.springframework</groupId>  
  8.             <artifactId>spring</artifactId>  
  9.         </exclusion>  
  10.         <exclusion>  
  11.             <groupId>org.springframework</groupId>  
  12.             <artifactId>spring-core</artifactId>  
  13.         </exclusion>  
  14.         <exclusion>  
  15.             <groupId>org.springframework</groupId>  
  16.             <artifactId>spring-web</artifactId>  
  17.         </exclusion>  
  18.         <exclusion>  
  19.             <groupId>org.springframework</groupId>  
  20.             <artifactId>spring-aop</artifactId>  
  21.         </exclusion>  
  22.         <exclusion>  
  23.             <groupId>org.springframework</groupId>  
  24.             <artifactId>spring-beans</artifactId>  
  25.         </exclusion>  
  26.         <exclusion>  
  27.             <groupId>org.springframework</groupId>  
  28.             <artifactId>spring-context</artifactId>  
  29.         </exclusion>  
  30.     </exclusions>  
  31. </dependency>  

  2:web.xml

 

Java代码  收藏代码
  1. <context-param>  
  2.         <param-name>contextConfigLocation</param-name>  
  3.         <param-value>  
  4.             classpath*:spring/*.xml  
  5.         </param-value>  
  6.     </context-param>  
  7.     <listener>  
  8.         <listener-class>  
  9.             org.springframework.web.context.ContextLoaderListener  
  10.         </listener-class>  
  11.   
  12.     </listener>  
  13.     <listener>  
  14.   
  15.         <listener-class>  
  16.             org.springframework.web.context.request.RequestContextListener  
  17.         </listener-class>  
  18.     </listener>  
  19.   
  20.   
  21.     <servlet>  
  22.         <servlet-name>JerseyServlet</servlet-name>  
  23.         <servlet-class>  
  24.             com.sun.jersey.spi.spring.container.servlet.SpringServlet  
  25.         </servlet-class>  
  26.         <init-param>  
  27.             <param-name>  
  28.                 com.sun.jersey.config.property.packages  
  29.             </param-name>  
  30.             <!-- 系统启动时扫描的包的路径-->    
  31.             <param-value>com.gissecur.mcas.webservices</param-value>  
  32.         </init-param>  
  33.         <init-param>  
  34.             <param-name>  
  35.                 com.sun.jersey.api.json.POJOMappingFeature  
  36.             </param-name>  
  37.             <param-value>true</param-value>  
  38.         </init-param>  
  39.   
  40.    
  41.         <load-on-startup>1</load-on-startup>  
  42.   
  43.     </servlet>  
  44.     <servlet-mapping>  
  45.         <servlet-name>JerseyServlet</servlet-name>  
  46.         <url-pattern>/resources/*</url-pattern>  
  47.     </servlet-mapping>  

 

 3:要生成rest的service类

  

Java代码  收藏代码
  1. package com.gissecur.mcas.webservices;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.ws.rs.GET;  
  5. import javax.ws.rs.Path;  
  6. import javax.ws.rs.PathParam;  
  7. import javax.ws.rs.Produces;  
  8. import javax.ws.rs.core.Context;  
  9. import javax.ws.rs.core.MediaType;  
  10.   
  11. import org.apache.commons.lang.exception.ExceptionUtils;  
  12. import org.apache.commons.logging.Log;  
  13. import org.apache.commons.logging.LogFactory;  
  14. import org.springframework.beans.factory.annotation.Autowired;  
  15. import org.springframework.beans.factory.annotation.Qualifier;  
  16. import org.springframework.context.annotation.Scope;  
  17. import org.springframework.stereotype.Component;  
  18.   
  19. import com.gissecur.mcas.exception.ServiceException;  
  20. import com.gissecur.mcas.model.TimebasedToken;  
  21. import com.gissecur.mcas.service.ChallengeService;  
  22. import com.gissecur.mcas.service.VerifyService;  
  23. import com.sun.jersey.spi.resource.Singleton;  
  24.   
  25. @Path("/tokenapi")  
  26. @Component  
  27. @Scope("request")  
  28. @Singleton  
  29. @SuppressWarnings("unqualified-field-access")  
  30. public class McasWebserviceTest {  
  31.     protected Log logger = LogFactory.getLog(getClass());  
  32.     @Autowired  
  33.     @Qualifier("challengeServiceImp")  
  34.     public ChallengeService challengeServiceImp;  
  35.   
  36.     @Autowired  
  37.     @Qualifier("verifyServiceImp")  
  38.     public VerifyService verifyServiceImp;  
  39.   
  40.     // 外部传过来的参数  
  41.     // @QueryParam("id") String serid;  
  42.   
  43.     public VerifyService getVerifyServiceImp() {  
  44.         return verifyServiceImp;  
  45.     }  
  46.   
  47.     public void setVerifyServiceImp(VerifyService verifyServiceImp) {  
  48.         this.verifyServiceImp = verifyServiceImp;  
  49.     }  
  50.   
  51.     @SuppressWarnings("nls")  
  52.     @GET  
  53.     @Scope("request")  
  54.     @Path("/hello")  
  55.     @Produces(MediaType.TEXT_PLAIN)  
  56.     public String helloWorld() { // @PathParam("username") String username  
  57.         String ret = "Hello World!";  
  58.         return ret;  
  59.     }  
  60.   
  61.     @SuppressWarnings( { "nls""unqualified-field-access" })  
  62.     @GET  
  63.     @Scope("request")  
  64.     @Produces(MediaType.TEXT_PLAIN)  
  65.     @Path("/crtchallengecode/{tokenid}")  
  66.     public String crtChallengeCode(@PathParam("tokenid")  
  67.     String tokenid, @Context  
  68.     HttpServletRequest request) {  
  69.         String retString = "";  
  70.         String clientIp = request.getRemoteAddr();  
  71.         System.out.println("token id in--------------->" + tokenid + "/"  
  72.                 + request.getRemoteAddr());  
  73.   
  74.         try {  
  75.             retString = challengeServiceImp.crtChgCode(tokenid, clientIp);  
  76.         } catch (Exception e) {  
  77.             // TODO Auto-generated catch block  
  78.             if (e instanceof ServiceException) {  
  79.                 ServiceException serverError = (ServiceException) e;  
  80.                 String errcode = serverError.getErrorCode();  
  81.                 String errMesage = serverError.getMessage();  
  82.                 retString = "challenge errorcode:" + errcode + "/descript:"  
  83.                         + errMesage;  
  84.   
  85.             } else {  
  86.                 retString = "challenge system error:" + e.getMessage();  
  87.             }  
  88.             logger.error("Challenge Code generate error->IP::" + clientIp  
  89.                     + ";tokenid::" + tokenid + ";"  
  90.                     + ExceptionUtils.getFullStackTrace(e));  
  91.             // throw new WebApplicationException(400);  
  92.         }  
  93.         return retString;  
  94.     }  
  95.   
  96.     @SuppressWarnings( { "nls""unqualified-field-access" })  
  97.     @GET  
  98.     @Scope("request")  
  99.     @Produces(MediaType.TEXT_PLAIN)  
  100.     @Path("/verify/{tokenid}/{challengecode}")  
  101.     public String verify(@PathParam("tokenid")  
  102.     String tokenid, @PathParam("challengecode")  
  103.     String challengecode, @Context  
  104.     HttpServletRequest request) {  
  105.         String retString = "";  
  106.         String clientIp = request.getRemoteAddr();  
  107.         try {  
  108.             TimebasedToken token = new TimebasedToken();  
  109.             token.setSerid(tokenid);  
  110.             token.setChgCode(challengecode);  
  111.             if (verifyServiceImp.tokenVerfiy(token, clientIp)) {  
  112.                 retString = "verify pass.";  
  113.             }  
  114.             else{  
  115.                 retString = "verify failed.";  
  116.             }  
  117.         } catch (Exception e) {  
  118.             // TODO Auto-generated catch block  
  119.             if (e instanceof ServiceException) {  
  120.                 ServiceException serverError = (ServiceException) e;  
  121.                 String errcode = serverError.getErrorCode();  
  122.                 String errMesage = serverError.getMessage();  
  123.                 retString = "verify errorcode:" + errcode + "/descript:"  
  124.                         + errMesage;  
  125.   
  126.             } else {  
  127.                 retString = "verify system error:" + e.getMessage();  
  128.             }  
  129.             logger  
  130.                     .error("verify generate error->IP::" + clientIp  
  131.                             + ";tokenid::" + tokenid + ";challenge code::"  
  132.                             + challengecode + ";"  
  133.                             + ExceptionUtils.getFullStackTrace(e));  
  134.             // throw new WebApplicationException(400);  
  135.         }  
  136.         return retString;  
  137.     }  
  138.   
  139.     @SuppressWarnings("unqualified-field-access")  
  140.     public ChallengeService getChallengeServiceImp() {  
  141.         return challengeServiceImp;  
  142.     }  
  143.   
  144.     public void setChallengeServiceImp(ChallengeService challengeServiceImp) {  
  145.         this.challengeServiceImp = challengeServiceImp;  
  146.     }  
  147.   
  148. }  

 

  访问方式:http://localhost:7001/mcas/resources/tokenapi/crtchallengecode/0000000001

原创粉丝点击