Building restful Web services based on Jersey and Apache Tomcat (ii)

Source: Internet
Author: User
Tags apache tomcat

Building restful Web services based on Jersey and Apache Tomcat (ii)

  The previous blog introduced rest and jersey and used it to build a simple "Hello world", so this time, a little more interesting things, of course, is also very simple, just include parameters in the path. Let's start with a hands-on practice.

Include parameters in the path

Next on the basis of the previous changes can be, or to add a method, arbitrary, this method is mainly to add input parameters in the path, and depending on the parameters, its return value is different, the return value is "Hello" + The parameters you entered. Here is the "Pathparam" parameter, the specific code is as follows:

1    @GET2         @Path ("/{username}")3        @Produces (mediatype.text_ PLAIN)4public          string Gethello (@PathParam ("username") string username) { 5             System.out.println (userName); 6             return "Hello" +userName; 7         }

This time using the restclient provided by Firefox to test, enter the path after the results are as follows:

  Yes, it's nice. But it seems that the string does not use a lot of, there are wood, is generally the return of the JSON format of data. So how do you get it to return data in JSON format?

Use Pojo and list to return the result in JSON format 

  The above is just a simple string return value, it's boring enough, here's a bit more interesting:

First, you need to add a jar package for processing JSON under Web-inf Lib, and then you need to create a people pojo, my Pojo as follows:

1 @XmlRootElement2  Public classpeople {3     4     PrivateString name;5     PrivateString password;6     7      PublicString GetName () {8         returnname;9     }Ten      Public voidsetName (String name) { One          This. Name =name; A     } -      PublicString GetPassword () { -         returnpassword; the     } -      Public voidSetPassword (String password) { -          This. Password =password; -} 

  This method only contains a simple Set/get method. Of course, don't forget to add jersey to this type of note:@XmlRootElement.

 Next, add a method to your class:

1 @GET2@Produces ("Application/json")3@Consumes ("Application/json")4          PublicList<people>Getpass () {5             6list<people> list =NewArraylist<people>();7People people =Newpeople ();8              for(inti = 0; I < 6; i++) {9                 TenPeople.setname ("Li" +i); OnePeople.setpassword ("123456" +i); A List.add (people); -             } -             returnlist; the}

  OK, here's the test, and as before, the input path will show the following result:

 

  Other methods such as Post, PUT, and delete are similar, no longer one by one demonstrations.

For more details, please refer to the following information: Https://jersey.java.net/documentation/latest/getting-started.html#new-from-archetype

PS: This blog Welcome to forward, but please specify the blog address and author, because I level limited, if there is wrong, welcome point, Thank you ~

Blog Address: http://www.cnblogs.com/voidy/

Blog: http://voidy.net

<. ))) ≦

Building restful Web services based on Jersey and Apache Tomcat (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.