Jersey在spring环境下的实现
2013-12-15 19:09
447 查看
目前项目用到Jersey 在这里记录一下
项目采用maven管理
1:pom.xml引得jersey相关jar
Java代码
<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代码
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.2</version>
</dependency>
Java代码
<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代码
<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代码
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
项目采用maven管理
1:pom.xml引得jersey相关jar
Java代码
<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>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代码
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.2</version>
</dependency>
<dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.2</version> </dependency>
Java代码
<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>
<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代码
<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>
<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代码
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;
}
}
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
相关文章推荐
- java 获取非堆内存使用量
- 成为Java高手的25个学习目标
- 对Java语言入门初学者的一些建议
- java前两章自己总结
- 2013/12/15 星期日:java 第三讲
- JAVA细粒度、互斥KEY锁 —— KeyLock
- java两种排序及二分查找
- ubuntu12.04 Dash home增添eclipse程序启动器
- java 加密 rsa例子
- eclipse 提示框颜色
- 继承多态绕点 Java篇
- Spring框架annotation实现IOC介绍
- 21 java 比较两个数的大小(oop)
- eclipse CreateProcess错误解决办法
- Spring 通过【构造函数】注入依赖
- 云计算(十七)- Windows下编译Hadoop2.2源码并引入eclipse
- 20 java 中快速生产 get set 方法
- hdu4165(卡特兰数)
- 十三章 上机练习
- 十三章 上机练习