Jersey building restful style of Webserivices (ii)

Source: Internet
Author: User

I. GENERAL description

XML and JSON are the most commonly used data interchange formats. This example shows how to turn a Java object into an XML output.

Second, the process

1. In the example above, create a package "Com.waylau.rest.bean"

2. Create a Java class "User" under the package

[Java]View Plaincopyprint?
  1. Package Com.waylau.rest.bean;
  2. Import javax.xml.bind.annotation.XmlRootElement;
  3. /*
  4. * User Bean
  5. * @author waylau.com
  6. * 2014-3-17
  7. */
  8. @XmlRootElement
  9. Public class User {
  10. private String userId;
  11. private String UserName;
  12. private String age;
  13. Public User () {};
  14. Public User (string userId, String userName, String age) {
  15. This.userid = userId;
  16. this.username = userName;
  17. this.age = age;
  18. }
  19. Public String getUserId () {
  20. return userId;
  21. }
  22. public void Setuserid (String userId) {
  23. This.userid = userId;
  24. }
  25. Public String GetUserName () {
  26. return userName;
  27. }
  28. public void Setusername (String userName) {
  29. this.username = userName;
  30. }
  31. Public String Getage () {
  32. return age;
  33. }
  34. public void Setage (String age) {
  35. this.age = age;
  36. }
  37. }

Note: This class adds an annotation "@XmlRootElement" that is the root node of the XML when it is converted to XML.

3. In "Com.waylau.rest.resources", add the Resource "Userresource" with the following code:

[Java]View Plaincopyprint?
  1. Package com.waylau.rest.resources;
  2. Import java.util.ArrayList;
  3. Import Java.util.HashMap;
  4. Import java.util.List;
  5. Import Java.util.Map;
  6. Import Javax.ws.rs.Path;
  7. Import javax.ws.rs.Produces;
  8. Import Javax.ws.rs.PathParam;
  9. Import Javax.ws.rs.core.MediaType;
  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 Com.waylau.rest.bean.User;
  15. @Path ("/users")
  16. Public class Userresource {
  17. private static map<string,user> UserMap = new hashmap<string,user> (); Store users
  18. /** 
  19. * Check All
  20. * @return
  21. */
  22. @GET
  23. @Produces (mediatype.application_xml)
  24. Public list<user> getAllUsers () {
  25. list<user> users = new arraylist<user> ();
  26. User U1 = new User ("001","Waylau","26");
  27. User U2 = new User ("002","King","23");
  28. User U3 = new User ("003","Susan","21");
  29. Usermap.put (U1.getuserid (), U1);
  30. Usermap.put (U2.getuserid (), U2);
  31. Usermap.put (U3.getuserid (), U3);
  32. Users.addall (Usermap.values ());
  33. return users;
  34. }
  35. @GET
  36. @Path ("/getuserxml")
  37. @Produces (mediatype.application_xml)
  38. Public User Getuserxml () {
  39. User user = new user ();
  40. User.setage ("21");
  41. User.setuserid ("004");
  42. User.setusername ("Amand");
  43. return user;
  44. }
  45. }

Where Mediatype.application_xml shows that the output is in XML form

Output a single object in the browser input http://localhost:8089/RestDemo/rest/users/getUserXml

A collection of Http://localhost:8089/RestDemo/rest/users output objects in the browser input

This article transferred from: http://blog.csdn.net/kkkloveyou/article/details/21415735

Jersey building restful style of Webserivices (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.