目前项目用到Jersey 在这里记录一下
项目采用maven管理
1:pom.xml引得jersey相关jar
<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>
<dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.2</version> </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
<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类
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
相关参考文章(感觉比较好的文章)
http://www.iteye.com/topic/1111932
http://www.cnblogs.com/bluesfeng/archive/2010/10/28/1863816.html
http://jersey.java.net/nonav/documentation/latest/user-guide.html#d4e919
http://jersey.java.net/nonav/documentation/latest/getting-started.html#d4e45