81,092
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>jaxrs</display-name>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<!-- 表示扫描哪个包中的资源,如有多个包的话用“;”分隔 -->
<param-value>net.blogjava.frankiegao123.resource</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<!-- REST 资源的顶层路径 -->
<url-pattern>/rs/*</url-pattern>
</servlet-mapping>
</web-app>
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
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.UriBuilder;
import javax.ws.rs.core.UriInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.blogjava.frankiegao123.bean.Link;
import net.blogjava.frankiegao123.bean.User;
@Path("/user")
public class UserResource {
private static Logger log = LoggerFactory.getLogger(UserResource.class);
@Context
private UriInfo info;
private static Map<Integer, User> database = new TreeMap<Integer, User>();
static {
innerAddUser(1, "Tom");
innerAddUser(2, "Jerry");
innerAddUser(3, "Mike");
innerAddUser(4, "Alice");
innerAddUser(5, "Frank");
}
private static User innerAddUser(Integer id, String name) {
User user = new User(id, name);
return innerAddUser(user);
}
private static User innerAddUser(User user) {
database.put(user.getId(), user);
return user;
}
@GET
@Produces("application/xml")
public List<User> getAllUsers() {
log.debug("get all user, request path: {}", info.getPath());
List<User> users = new LinkedList<User>(database.values());
UriBuilder builder = info.getAbsolutePathBuilder();
builder.path("{id}");
for(User user : users) {
user.setLink(new Link(builder.clone().build(user.getId()).toString(), "application/xml"));
}
return users;
}
@GET
@Produces("application/xml")
@Path("{id}")
public User getUser(@PathParam("id") int id) {
User user = database.get(id);
log.debug("get {} user: {}, request path: {}, ", new Object[]{ id, user, info.getPath() });
return user;
}
@POST
@Produces("application/xml")
@Path("{id}/{name}")
public User updateUser(@PathParam("id") int id, @PathParam("name") String name) {
User user = database.get(id);
if(user == null) {
log.info("update user failed, id: {}, user isnot exist that cannot be update, request path: {}", id, info.getPath());
return null;
}
user = innerAddUser(id, name);
log.debug("update user ok, id: {}, user: {}, request path: {}", new Object[]{ id, user, info.getPath() });
return user;
}
@PUT
@Produces("application/xml")
@Path("{id}/{name}")
public User addUser(@PathParam("id") int id, @PathParam("name") String name) {
User user =database.get(id);
if(user != null) {
log.info("add user failed, id: {}, user has exist that cannot be add, request path: {}", id, info.getPath());
return null;
}
user = innerAddUser(id, name);
log.debug("add user ok, id: {}, user: {}, request path: {}", new Object[]{ id, user, info.getPath() });
return user;
}
@DELETE
@Produces("application/xml")
@Path("{id}")
public User removeUser(@PathParam("id") int id) {
User user = database.get(id);
if(user == null) {
log.info("remove user failed, id: {}, user has exist that cannot be delete, request path: {}", id, info.getPath());
return null;
}
user = database.remove(id);
log.debug("remove user ok, id: {}, user: {}, request path: {}", new Object[]{ id, user, info.getPath() });
return user;
}
}
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "link")
public class Link {
protected String href;
protected String type;
public Link() {
}
public Link(String href, String type) {
this.href = href;
this.type = type;
}
@XmlAttribute
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
@XmlAttribute
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "user")
public class User {
private Link link;
private Integer id;
private String name;
public User() {
}
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
public Link getLink() {
return link;
}
public void setLink(Link link) {
this.link = link;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
<users>
<user>
<id>1</id>
<link type="application/xml" href="http://localhost:8080/jaxrs/rs/user/1"/>
<name>Tom</name>
</user>
<user>
<id>2</id>
<link type="application/xml" href="http://localhost:8080/jaxrs/rs/user/2"/>
<name>Jerry</name>
</user>
<user>
<id>3</id>
<link type="application/xml" href="http://localhost:8080/jaxrs/rs/user/3"/>
<name>Mike</name>
</user>
<user>
<id>4</id>
<link type="application/xml" href="http://localhost:8080/jaxrs/rs/user/4"/>
<name>Alice</name>
</user>
<user>
<id>5</id>
<link type="application/xml" href="http://localhost:8080/jaxrs/rs/user/5"/>
<name>Frank</name>
</user>
</users>