Jersey Building Rest Service style

Source: Internet
Author: User

Here we introduce how to update the list array with the Post method of rest, the related concepts of rest can be referred to
http://blog.csdn.net/lfsfxy9/article/details/9205337

Since the service is provided, there must be both service side and cient side. This will use some of the classes encapsulated by the Jersey framework to showcase a rest service-style server and client.

Service side

' @POST @path ("/updatepwd") @Produces ({mediatype.application_xml, Mediatype.application_json}) @Consumes (" Application/x-www-form-urlencoded ") @SuppressWarnings ({" Unchecked "," rawtypes "}) public Response updatepwd ( Multivaluedmap<string, list<string>> UserMap) throws Serviceexception {//Checksum pass parameter if ("0". Equals (" Usermap.size ()) {Servicerevisionutils.registererror () {status_ict_err_param, "pass the required parameter to NULL error, please verify that the parameter is mandatory. Information such as the length of the parameter.    "); } if (Stringutils.isempty (Usermap.get ("token"). ToString ())) {Servicerevisionutils.registererror (status_ict_err_ PARAM, "Pass the required parameter is a null error, please verify that the parameters are required to fill the non-required, parameter length and other information."    ");    } HashMap Accountpwdmap = new hashmap<string, string> ();    List userlist=new arraylist<string> ();    Userlist=usermap.get ("userlist");        for (int i=0;i<userlist.size (); i++) {string[] Users=userlist.get (i). ToString (). Split (",");    Accountpwdmap.put (Users[0],users[1]); } if (userinforevisionmanager.updatepwd(Accountpwdmap))                        {return Response.ok (//Status value 0 indicates a social Security number "{\" status\ ": 1, \" message\ ": \" "+ usermap.size () + "Bar Password update successful!    "+" \ "}"). Build (); } else {return Response.ok (//Status value 0 indicates a social Security number "{\" status\ ": 2, \" message\ ": \" + "Password update failed to rollback please try again!)    "+" \ "}"). Build (); }}`

Client Side

`public static void main(String[] args) {    ClientConfig cc = new DefaultClientConfig();    Client c = Client.create(cc);    MultivaluedMap form = new MultivaluedMapImpl();    form.add("token", "b310e7cb766146d28caa458fc34ed62a");    List userList=new ArrayList<String>();          userList.add("super,1");    userList.add("XPKY01SG03,2");    form.put("userList",userList);    String baseUrl = "http://localhost:8080/baseconnect/rest/fieldServiceRevision/updatePwd";    WebResource wr = c.resource(baseUrl);    String response = wr.post(String.class, form);    System.out.println("result:" + response);}`

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Jersey Building Rest Service style

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.