您的位置:首页 > 运维架构 > Tomcat

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
<?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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息