1. Person类
package com.base.pf.restful.client;
import java.io.Serializable;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Person implements Serializable {
private static final long serialVersionUID = -8039686696076337053L;
private Long id;
private String name;
private Integer age;
private String description;
private Date birthday;
public Person(String name, Integer age, Date birthday, String description) {
this.name = name;
this.age = age;
this.birthday = birthday;
this.description = description;
}
public Person(Long id, String name, Integer age, Date birthday,
String description) {
this.id = id;
this.name = name;
this.age = age;
this.birthday = birthday;
this.description = description;
}
public String toString() {
return "{id:"+id+",name:"+name+",age:"+age+",birthday:"+birthday+",decription:"+description+"}";
}
public Person() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
2. 服务端
package com.base.pf.restful;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
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 net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.base.pf.restful.client.Person;
/**
* jersey 实现增删改查
*
* @author ZHEN.L
*
*/
@Path("person")
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML,MediaType.TEXT_XML})
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML,MediaType.TEXT_XML})
public class PersonResource {
static Map<Long, Person> map = new HashMap<Long, Person>();
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
"yyyy-mm-dd");
public PersonResource() {
Person p;
try {
p = new Person(10000L, "张三", 18, DATE_FORMAT.parse("1998-12-10"),
"好学生。");
map.put(p.getId(), p);
p = new Person(10001L, "李四", 19, DATE_FORMAT.parse("1997-05-08"),
"体育优秀。");
map.put(p.getId(), p);
p = new Person(10002L, "王五", 20, DATE_FORMAT.parse("1996-10-01"),
"数学优秀。");
map.put(p.getId(), p);
} catch (ParseException e) {
e.printStackTrace();
}
}
/**
* 查询列表
*
* @return
*/
@GET
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public String getPersons() {
Collection<Person> persons = (Collection<Person>) map.values();
List<Person> list = new ArrayList<Person>(persons);
return JSONArray.fromObject(list).toString();
}
/**
* 查询单条记录
*
* @param id
* @return
*/
@GET
@Path("{id}")
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public String getPerson(@PathParam("id") Long id) {
return JSONObject.fromObject(map.get(id)).toString();
}
/**
* 新建
*
* @param person
* @return
*/
@POST
@Path("savePerson")
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
@Consumes("application/x-www-form-urlencoded")
public String savePerson(String params) {
Person person = (Person) JSONObject.toBean(JSONObject.fromObject(params),Person.class);
Double d = Math.random() * 10000;
Long id = d.longValue();
person.setId(id);
map.put(id, person);
Set<Long> ids = map.keySet();
Iterator<Long> its = ids.iterator();
while(its.hasNext()){
System.out.println(map.get(its.next()));
}
return JSONObject.fromObject(person).toString();
}
/**
* 修改
*
* @param person
* @return
*/
@PUT
@Path("update")
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public String updatePerson(String params) {
Person person = (Person) JSONObject.toBean(JSONObject.fromObject(params),Person.class);
map.put(person.getId(), person);
return JSONObject.fromObject(person).toString();
}
/**
* 删除
*
* @param id
*/
@DELETE
@Path("{id}")
public void deletePerson(@PathParam("id") Long id) {
map.remove(id);
}
}
3. 客户端调用
package com.base.pf.restful.client;
import java.util.Date;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
public class JerseyClient {
/**
* 获取单条记录
*/
public void findPerson() {
Client c = Client.create();
WebResource resource = c
.resource("http://localhost:8080/security/rest/person/10000");
String data = resource.get(String.class);
Person p = (Person) JSONObject.toBean(JSONObject.fromObject(data),
Person.class);
// Person p = resource.get(new GenericType<Person>() {
// });
System.out.print(p.getId());
System.out.print(", " + p.getName());
System.out.print(", " + p.getAge());
System.out.print(", " + p.getBirthday());
System.out.print(", " + p.getDescription());
System.out.println();
}
/**
* 获取列表
*/
@SuppressWarnings("unchecked")
public void findPersons() {
Client c = Client.create();
WebResource resource = c
.resource("http://localhost:8080/security/rest/person");
String data = resource.get(String.class);
List<Person> persons = JSONArray.toList(JSONArray.fromObject(data),
Person.class);
// List<Person> list = resource.get(new GenericType<List<Person>>() {
// });
for (Person p : persons) {
System.out.print(p.getId());
System.out.print(", " + p.getName());
System.out.print(", " + p.getAge());
System.out.print(", " + p.getBirthday());
System.out.print(", " + p.getDescription());
System.out.println();
}
System.out.println();
}
/**
* 新建人员
*/
public void savePerson() {
Client c = Client.create();
WebResource webResource = c
.resource("http://localhost:8080/security/rest/person/savePerson");
Person p = new Person("老王", 36, new Date(), "隔壁老王。");
String data = webResource.post(String.class, JSONObject.fromObject(p)
.toString());
Person person = (Person) JSONObject.toBean(JSONObject.fromObject(data),
Person.class);
System.out.print(person.getId());
System.out.print(", " + person.getName());
System.out.print(", " + person.getAge());
System.out.print(", " + person.getBirthday());
System.out.print(", " + person.getDescription());
System.out.println();
}
/**
* 修改人员
*/
public void updatePerson() {
Client c = Client.create();
WebResource webResource = c.resource("http://localhost:8080/security/rest/person/update");
Person p = new Person("老王", 88, new Date(), "隔壁老王up。");
p.setId(10000L);
String data = webResource.put(String.class,JSONObject.fromObject(p).toString());
Person person = (Person) JSONObject.toBean(JSONObject.fromObject(data),
Person.class);
System.out.print(person.getId());
System.out.print(", " + person.getName());
System.out.print(", " + person.getAge());
System.out.print(", " + person.getBirthday());
System.out.print(", " + person.getDescription());
System.out.println();
}
/**
* 删除人员
*/
public void deletePerson(){
Client c = Client.create();
WebResource webResource = c.resource("http://localhost:8080/security/rest/person/10000");
webResource.delete();
}
public static void main(String[] args) {
JerseyClient c = new JerseyClient();
// c.findPerson();
c.findPersons();
System.out.println(" ----------------------- ");
// c.updatePerson();
// c.savePerson();
c.deletePerson();
System.out.println(" ----------------------- ");
c.findPersons();
}
}