目前项目用到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
分享到:
相关推荐
代码来自互联网,经过整理测试可用;大家注意里面的说明文档
jersey加入spring支持 jersey加入spring支持
java应用程序实现jetty 集成jersey 和spring
jersey加spring加mybatis整合,支持json转换和事物管理
com.sun.jersey.spi.spring.container.servlet.SpringServlet需要此包的支持,使用spring3.1.1开发web service project时,用于添加Spring框架支持,使用IOC容器接管所有对象实例。解决在web service project中使用...
一个Jersey+Spring框架的示例。
NULL 博文链接:https://xosadan.iteye.com/blog/1119235
jersey-spring4-2.26.jar 最新版本spring4-2.26,最新版本spring4-2.26
Jersey-spring3-2.4.1.jar
jersey整合spring示例,各种jar包报错,搞了俩小时。
Jersey+Spring:解决Jersey单实例问题
Jersey2.13+spring3.2.3入门实例,包含源码及tomcat webapp. 运行环境: myeclipse10.6 tomcat7.0+jdk1.7
Spring4整合Jersey2.9的入门案例,采用Maven构建
基于 spring2.5 hibernate3.3 jpa 的annotation注解来减少配置文件的大小 通过jersey实现restful形式请求
本人亲测试,没有问题。实现spring4.x 和jersey2.2x 集成实现对外提供服务
jersey-spring-1.17.ja
由于项目的需要,自己利用闲暇的时间在外部搭建了一个jersey和spring简单集成的环境,后期会随着相关业务的加入会变得更加庞大。1、环境的搭建(开发工具:eclipse,个人喜好吧,ide现期比较适应eclipse)对于jersey...
下载后里面的target目录下有war包,可以直接放在tomcat下运行。 项目使用Intellij Idea 14 创建。 http://localhost:8080/rest/helloWorld/a 单独的jersey http://localhost:8080/rest/demo/hello 集成spring的...
java jersey spring 集成 开发 restful API ,并将restful 返回的数据以JSON的格式返回,开发非常简单,易懂。欢迎小伙伴们 下载资源。
jersey+spring+hibernate配置