基于jersey的Rest风格学习笔记1

约定:

使用RestClient对代码进行测试,下载地址:

https://addons.mozilla.org/en-US/firefox/addon/restclient/versions/?page=1#version-1.3.5

资源类:

package com.lemon.resource;
import java.util.ArrayList;
import javax.ws.rs.Consumes;
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.MediaType;
import javax.ws.rs.core.Response;

import com.lemon.manager.UserInfoManager;
import com.lemon.model.UserInfoModel;

@Path("/user")
public class UserInfoResource {

	@GET
	@Path("/{userId}/get")
	@Produces({"application/xml", "application/json"})
	public UserInfoModel getUser(@PathParam("userId") String userId){
		UserInfoManager manager=new UserInfoManager();
		return manager.getUser(userId);
	}
	
	@GET
	@Path("/users")
	@Produces({"application/xml", "application/json"})
	public ArrayList<UserInfoModel> listUser(){
		UserInfoManager manager=new UserInfoManager();
		return manager.listAllUser();
	}
	
	@PUT
	@Path("/update")
	@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	@Consumes( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public UserInfoModel updateUser(UserInfoModel user){
		UserInfoManager manager=new UserInfoManager();
		return manager.updateUser(user);
	}
	
	@POST
	@Path("/create")
	@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	@Consumes( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public UserInfoModel createUser(UserInfoModel user){
		UserInfoManager manager=new UserInfoManager();
		return manager.createUser(user);
	}
	
	@DELETE
	@Path("/{userId}/del")
	@Produces( { "application/xml", "application/json" })
	public Response deleteUser(@PathParam("userId")String userId){
		UserInfoManager manager=new UserInfoManager();
		manager.deleteUser(userId);
		return Response.ok().build();
	}
	
}

Model类:

package com.lemon.model;

import java.sql.Timestamp;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessType;
import com.lemon.util.TimeUtil;
@XmlRootElement(name="userInfoModel")
@XmlAccessorType(XmlAccessType.NONE)
public class UserInfoModel {
	private Integer userId;
	private String userName;
	private Timestamp birthday;
	public UserInfoModel() {
	}
	public UserInfoModel(Integer userId, String userName, Timestamp birthday) {
		this.userId = userId;
		this.userName = userName;
		this.birthday = birthday;
	}
	@XmlElement
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	@XmlElement
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	
	public Timestamp getBirthday() {
		return birthday;
	}
	public void setBirthday(Timestamp birthday) {
		this.birthday = birthday;
	}
	@XmlElement(name="birthday")
	public String getBirthdayStr() throws Exception{
		if(birthday!=null){
			return TimeUtil.getDateString(birthday.getTime());
		}
		return null;
	}
	
}

TimeUtil类:

package com.lemon.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtil {
	public static String getDateString(long timeMillis) {

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date(timeMillis);
		String mydate = format.format(date);
		return mydate;
	}
}

web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<!-- configuration for Rest begin -->
	<servlet>
		<servlet-name>GeneralRestServlet</servlet-name>
		<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
	</servlet>
	<servlet>
		<servlet-name>DojoRestServlet</servlet-name>
		<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>GeneralRestServlet</servlet-name>
		<url-pattern>/rest/general/*</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>DojoRestServlet</servlet-name>
		<url-pattern>/rest/dojo/*</url-pattern>
	</servlet-mapping>
	<!-- configuration for Rest end -->
</web-app>

在测试之前,需要设置下RestClient的Http请求报头:

Content-type:application/xml


你可能感兴趣的:(REST,jersey,完整示例)