Jersey 极致简单的Restful WebService实现
2012-09-24 11:08
387 查看
关于Jersey 的介绍网上很多很多,感兴趣自己搜吧,这里直接上代码比较直观:
1.创建DTO:UserDTO
package org.tshark.framework.ws.user;
import javax.xml.bind.annotation.XmlType;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.tshark.framework.ws.WsConstants;
@XmlType(name = "User", namespace = WsConstants.NAME_SPACE)
public class UserDTO {
private Long id;
private String loginName;
private String name;
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
2. 创建WebService实现类
package org.tshark.framework.ws.user;
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 javax.ws.rs.core.UriInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.tshark.core.exception.TSharkException;
import org.tshark.framework.ws.WsConstants;
@Path("/users")
public class UserWebService {
protected final Log log = LogFactory.getLog(getClass());
@Context
private UriInfo uriInfo;
@GET
@Path("{id}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML + WsConstants.CHARSET })
public UserDTO getUser(@PathParam("id") Long id) {
try {
log.debug("GetUser Id:" + id);
UserDTO user = new UserDTO();
user.setId(1l);
user.setLoginName("LoginName");
user.setName("Tiger");
user.setEmail("Tiger email");
return user;
} catch (RuntimeException e) {
log.error(e.getMessage(), e);
throw new TSharkException(e);
}
}
}
3.调用客户端:
package org.tshark.framework.ws.user;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
public class UserClient {
private WebResource client = Client.create().resource("http://localhost:8888/tshark2/rs");
/**
* 获取用户.
*/
public UserDTO getUser(Long id) {
UserDTO user = client.path("/users/" + id).accept(MediaType.APPLICATION_JSON).get(UserDTO.class);
//client.addFilter(new HTTPBasicAuthFilter("tomcat", "tomcat"));
System.out.println(user);
return user;
}
public static void main(String[] args) {
UserClient userClient = new UserClient();
userClient.getUser(1l);
}
}
4.配置文件
web.xml
<!-- Jersey 配置 -->
<servlet>
<servlet-name>JerseyServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JerseyServlet</servlet-name>
<url-pattern>/rs/*</url-pattern>
</servlet-mapping>
applicationContext-rs-server.xml
1.创建DTO:UserDTO
package org.tshark.framework.ws.user;
import javax.xml.bind.annotation.XmlType;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.tshark.framework.ws.WsConstants;
@XmlType(name = "User", namespace = WsConstants.NAME_SPACE)
public class UserDTO {
private Long id;
private String loginName;
private String name;
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
2. 创建WebService实现类
package org.tshark.framework.ws.user;
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 javax.ws.rs.core.UriInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.tshark.core.exception.TSharkException;
import org.tshark.framework.ws.WsConstants;
@Path("/users")
public class UserWebService {
protected final Log log = LogFactory.getLog(getClass());
@Context
private UriInfo uriInfo;
@GET
@Path("{id}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML + WsConstants.CHARSET })
public UserDTO getUser(@PathParam("id") Long id) {
try {
log.debug("GetUser Id:" + id);
UserDTO user = new UserDTO();
user.setId(1l);
user.setLoginName("LoginName");
user.setName("Tiger");
user.setEmail("Tiger email");
return user;
} catch (RuntimeException e) {
log.error(e.getMessage(), e);
throw new TSharkException(e);
}
}
}
3.调用客户端:
package org.tshark.framework.ws.user;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
public class UserClient {
private WebResource client = Client.create().resource("http://localhost:8888/tshark2/rs");
/**
* 获取用户.
*/
public UserDTO getUser(Long id) {
UserDTO user = client.path("/users/" + id).accept(MediaType.APPLICATION_JSON).get(UserDTO.class);
//client.addFilter(new HTTPBasicAuthFilter("tomcat", "tomcat"));
System.out.println(user);
return user;
}
public static void main(String[] args) {
UserClient userClient = new UserClient();
userClient.getUser(1l);
}
}
4.配置文件
web.xml
<!-- Jersey 配置 -->
<servlet>
<servlet-name>JerseyServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JerseyServlet</servlet-name>
<url-pattern>/rs/*</url-pattern>
</servlet-mapping>
applicationContext-rs-server.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd" default-lazy-init="true"> <description>Jersey Restful Service Bean的配置文件 </description> <bean class="org.tshark.framework.ws.user.UserWebService" /> </beans>
相关文章推荐
- spring4+springMVC+mybatis集成jersey2,一个实现了RESTful的webservice框架。(源码)
- 用Spring+cxf 实现一个简单的restful webService
- 使用java实现Restful webservice(Jersey)
- 使用Jersey框架实现RESTfulWebService
- 一个实例演示jersey框架实现RESTful风格的Webservice,并发布到Tomcat,以及客户端调用示范
- 【jersey】 spring 整合jersey 实现RESTful webservice
- Jersey实现Restful webservice及Ajax访问
- RestfulWebService实现之一--------------Jetty+Jersey方法一
- 在同一个系统里用cxf 实现SOAP 协议和RESTful风格 两种类型的webservice接口(含简单demo)
- cxf实现webservice简单插入,查询,和返回结构集
- Spring Boot & Apache CXF——简单的webservice,并实现用户验证
- .net请求Webservice简单实现天气预报功能
- Jetty + Jersey简单RESTful例子
- 使用 jersey构建RESTful的WebService
- WebService的简单实现
- webservice使用CXF的简单实现
- 用Node.js实现Restful风格webservice
- Restful WebService简单介绍
- JDK实现WebService简单实例
- 使用CXF框架实现webservice的一个简单例子