一、总体说明
通过jersey-client接口,创建客户端程序,来调用Jersey实现的RESTful服务,实现增、删、改、查等操作。
服务端主要是通过内存的方式,来模拟用户的增加、删除、修改、查询等操作。
二、创建服务端
1.在上文项目中,
在“cn.itrush.rest.resources“包中增加UserResource1.java类,
2.创建增、删、改、查 用户资源等操作
1 package cn.itrush.rest.resources; 2 import java.util.ArrayList; 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Map; 7 import java.util.Map.Entry; 8 9 import javax.ws.rs.Consumes; 10 import javax.ws.rs.DELETE; 11 import javax.ws.rs.GET; 12 import javax.ws.rs.POST; 13 import javax.ws.rs.PUT; 14 import javax.ws.rs.Path; 15 import javax.ws.rs.PathParam; 16 import javax.ws.rs.Produces; 17 import javax.ws.rs.core.MediaType; 18 19 import cn.itrush.rest.bean.User; 20 21 /** 22 * 回嘉 23 * @author huiqinbo.cn 24 * 2016-8-24 25 */ 26 @Path("users1") 27 public class UserResource1 { 28 public static Map<String,User> userMap = new HashMap<String,User>(); 29 30 @GET 31 @Produces(MediaType.APPLICATION_XML) 32 @Path("{id}") 33 public User queryUserById(@PathParam("id") String id){ 34 System.out.println("***按id查询***"); 35 return userMap.get(id); 36 } 37 38 39 @GET 40 @Produces(MediaType.APPLICATION_XML) 41 public List<User> queryAllUser(){ 42 System.out.println("***查询所有用户***"); 43 List<User> users = new ArrayList<User>(); 44 users.addAll(userMap.values()); 45 return users; 46 } 47 48 49 @POST 50 @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 51 public void addUser(User user){ 52 System.out.println("***增加用户***"); 53 userMap.put(user.getUserId(), user); 54 } 55 56 57 @DELETE 58 @Path("{id}") 59 public void deleteUser(@PathParam("id") String id){ 60 System.out.println("***删除用户***"); 61 userMap.remove(id); 62 } 63 64 @PUT 65 @Produces(MediaType.APPLICATION_XML) 66 public void updateUser(User user){ 67 System.out.println("***更新用户***"); 68 userMap.put(user.getUserId(), user); 69 } 70 }
三、创建客户端程序
1.创建包“cn.itrush.rest.client”,在包下建一个ClientTest.Java,代码如下:
1 package cn.itrush.rest.client; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.Map; 6 import java.util.Map.Entry; 7 8 import javax.ws.rs.client.Client; 9 import javax.ws.rs.client.ClientBuilder; 10 import javax.ws.rs.client.Entity; 11 import javax.ws.rs.client.WebTarget; 12 import javax.ws.rs.core.MediaType; 13 import javax.ws.rs.core.Response; 14 15 import org.codehaus.jackson.jaxrs.JacksonJsonProvider; 16 17 import cn.itrush.rest.bean.User; 18 19 /** 20 * 回嘉 21 * @author huiqinbo.cn 22 * 2016-8-25 23 */ 24 public class ClientTest { 25 public static void main(String args[]){ 26 add(); 27 update(); 28 // delete(); 29 query(); 30 // testMapMothed(); 31 } 32 33 private static void add(){ 34 // Client client = ClientBuilder.newClient().register(JacksonJsonProvider.class); //客户端如果需要进行JSON转换,需要进行JSON注册 35 Client client = ClientBuilder.newClient(); 36 WebTarget target = client.target("http://localhost:8082/RestDemo/rest/users1"); 37 User user = new User("002","Andy","20"); 38 Response response = target.request().buildPost(Entity.entity(user, MediaType.APPLICATION_XML)).invoke(); 39 response.close(); 40 } 41 private static void query(){ 42 Client client = ClientBuilder.newClient().register(JacksonJsonProvider.class); 43 WebTarget target = client.target("http://localhost:8082/RestDemo/rest/users1/002"); 44 Response response1 = target.request().get(); 45 User u = response1.readEntity(User.class); 46 if(u == null ||u.equals(null)){ 47 System.out.println("没有查询的内容"); 48 response1.close(); 49 }else{ 50 System.out.println(u.getUserId()); 51 System.out.println(u.getUserName()); 52 System.out.println(u.getAge()); 53 54 } 55 } 56 private static void delete(){ 57 Client client = ClientBuilder.newClient(); 58 WebTarget target = client.target("http://localhost:8082/RestDemo/rest/users1/002"); 59 Response response = target.request().delete(); 60 response.close(); 61 } 62 63 private static void update(){ 64 User user = new User(); 65 user.setUserId("002"); 66 user.setUserName("回嘉"); 67 user.setAge("30"); 68 Client client = ClientBuilder.newClient(); 69 WebTarget target = client.target("http://localhost:8082/RestDemo/rest/users1"); 70 Response response = target.request().buildPut(Entity.entity(user, MediaType.APPLICATION_XML)).invoke(); 71 response.close(); 72 } 73 74 private static void testMapMothed(){ 75 Map<String , Object> maps = new HashMap<String,Object>(); 76 User user = new User("1","an","20"); 77 maps.put("002",user ); 78 User user1 = new User("2","and","30"); 79 maps.put("003",user1); 80 81 Iterator i = maps.entrySet().iterator(); 82 while(i.hasNext()){ 83 Map.Entry map = (Entry) i.next(); 84 System.out.println(map.getKey()); 85 User useri = (User) map.getValue(); 86 System.out.println(useri.getUserName()); 87 } 88 } 89 90 }
四、运行
以main方式启动服务端项目,运行客户端程序ClientTest,控制台输出如下
服务器端显示:
五、总结
1.客户端如果需要进行JSON转换,需要进行JSON注册
Client client = ClientBuilder.newClient().register(JacksonJsonProvider.class);
2.WebTarget 指明了要请求的资源的地址
3.target.request(). 后面跟的是请求的方法:POST,GET,PUT或DELETE
参考文献:http://blog.csdn.net/kkkloveyou/article/details/21517165
我是一位基督徒,感谢耶和华的爱,耶稣的恩惠,圣灵的感动常与我同在!