Jersey在spring环境下的实现

目前项目用到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>  
	<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-core</artifactId>
			<version>1.11</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-server</artifactId>
			<version>1.11</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-client</artifactId>
			<version>1.11</version>
		</dependency>

		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-json</artifactId>
			<version>1.10</version>
		</dependency>
  		<dependency>
  			 <groupId>javax.ws.rs</groupId>
  			 <artifactId>jsr311-api</artifactId>
  			 <version>1.1.1</version>
  		</dependency>
Java代码 复制代码
  1.           <dependency>   
  2.                <groupId>asm</groupId>   
  3.              <artifactId>asm</artifactId>   
  4.              <version>3.2</version>   
  5.         </dependency>  
  		<dependency>
  			 <groupId>asm</groupId>
   			<artifactId>asm</artifactId>
 			  <version>3.2</version>
 		 </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>  
		<dependency>
			<groupId>com.sun.jersey.contribs</groupId>
			<artifactId>jersey-spring</artifactId>
			<version>1.11</version>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring</artifactId>
				</exclusion>
				<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-aop</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-beans</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-context</artifactId>
				</exclusion>
			</exclusions>
		</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>  
<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:spring/*.xml
		</param-value>
	</context-param>
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>

	</listener>
	<listener>

		<listener-class>
			org.springframework.web.context.request.RequestContextListener
		</listener-class>
	</listener>


	<servlet>
		<servlet-name>JerseyServlet</servlet-name>
		<servlet-class>
			com.sun.jersey.spi.spring.container.servlet.SpringServlet
		</servlet-class>
		<init-param>
			<param-name>
				com.sun.jersey.config.property.packages
			</param-name>
			<!-- 系统启动时扫描的包的路径-->  
			<param-value>com.gissecur.mcas.webservices</param-value>
		</init-param>
		<init-param>
			<param-name>
				com.sun.jersey.api.json.POJOMappingFeature
			</param-name>
			<param-value>true</param-value>
		</init-param>

 
		<load-on-startup>1</load-on-startup>

	</servlet>
	<servlet-mapping>
		<servlet-name>JerseyServlet</servlet-name>
		<url-pattern>/resources/*</url-pattern>
	</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. }  
package com.gissecur.mcas.webservices;

import javax.servlet.http.HttpServletRequest;
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.Context;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.gissecur.mcas.exception.ServiceException;
import com.gissecur.mcas.model.TimebasedToken;
import com.gissecur.mcas.service.ChallengeService;
import com.gissecur.mcas.service.VerifyService;
import com.sun.jersey.spi.resource.Singleton;

@Path("/tokenapi")
@Component
@Scope("request")
@Singleton
@SuppressWarnings("unqualified-field-access")
public class McasWebserviceTest {
	protected Log logger = LogFactory.getLog(getClass());
	@Autowired
	@Qualifier("challengeServiceImp")
	public ChallengeService challengeServiceImp;

	@Autowired
	@Qualifier("verifyServiceImp")
	public VerifyService verifyServiceImp;

	// 外部传过来的参数
	// @QueryParam("id") String serid;

	public VerifyService getVerifyServiceImp() {
		return verifyServiceImp;
	}

	public void setVerifyServiceImp(VerifyService verifyServiceImp) {
		this.verifyServiceImp = verifyServiceImp;
	}

	@SuppressWarnings("nls")
	@GET
	@Scope("request")
	@Path("/hello")
	@Produces(MediaType.TEXT_PLAIN)
	public String helloWorld() { // @PathParam("username") String username
		String ret = "Hello World!";
		return ret;
	}

	@SuppressWarnings( { "nls", "unqualified-field-access" })
	@GET
	@Scope("request")
	@Produces(MediaType.TEXT_PLAIN)
	@Path("/crtchallengecode/{tokenid}")
	public String crtChallengeCode(@PathParam("tokenid")
	String tokenid, @Context
	HttpServletRequest request) {
		String retString = "";
		String clientIp = request.getRemoteAddr();
		System.out.println("token id in--------------->" + tokenid + "/"
				+ request.getRemoteAddr());

		try {
			retString = challengeServiceImp.crtChgCode(tokenid, clientIp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			if (e instanceof ServiceException) {
				ServiceException serverError = (ServiceException) e;
				String errcode = serverError.getErrorCode();
				String errMesage = serverError.getMessage();
				retString = "challenge errorcode:" + errcode + "/descript:"
						+ errMesage;

			} else {
				retString = "challenge system error:" + e.getMessage();
			}
			logger.error("Challenge Code generate error->IP::" + clientIp
					+ ";tokenid::" + tokenid + ";"
					+ ExceptionUtils.getFullStackTrace(e));
			// throw new WebApplicationException(400);
		}
		return retString;
	}

	@SuppressWarnings( { "nls", "unqualified-field-access" })
	@GET
	@Scope("request")
	@Produces(MediaType.TEXT_PLAIN)
	@Path("/verify/{tokenid}/{challengecode}")
	public String verify(@PathParam("tokenid")
	String tokenid, @PathParam("challengecode")
	String challengecode, @Context
	HttpServletRequest request) {
		String retString = "";
		String clientIp = request.getRemoteAddr();
		try {
			TimebasedToken token = new TimebasedToken();
			token.setSerid(tokenid);
			token.setChgCode(challengecode);
			if (verifyServiceImp.tokenVerfiy(token, clientIp)) {
				retString = "verify pass.";
			}
			else{
				retString = "verify failed.";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			if (e instanceof ServiceException) {
				ServiceException serverError = (ServiceException) e;
				String errcode = serverError.getErrorCode();
				String errMesage = serverError.getMessage();
				retString = "verify errorcode:" + errcode + "/descript:"
						+ errMesage;

			} else {
				retString = "verify system error:" + e.getMessage();
			}
			logger
					.error("verify generate error->IP::" + clientIp
							+ ";tokenid::" + tokenid + ";challenge code::"
							+ challengecode + ";"
							+ ExceptionUtils.getFullStackTrace(e));
			// throw new WebApplicationException(400);
		}
		return retString;
	}

	@SuppressWarnings("unqualified-field-access")
	public ChallengeService getChallengeServiceImp() {
		return challengeServiceImp;
	}

	public void setChallengeServiceImp(ChallengeService challengeServiceImp) {
		this.challengeServiceImp = challengeServiceImp;
	}

}

 

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值