Java's resttemplate Access application

Source: Internet
Author: User

One, rest is representational state passing (English: representational, Transfer, or rest) is a software architecture style proposed by Dr. Roy Fielding in his doctoral dissertation in 2000. It is a design and development method for network application, which can reduce the complexity of development and increase the scalability of the system.

There are currently three mainstream Web service implementations, as the rest-mode Web services are significantly more concise compared to the complex soap and XML-RPC, and more and more Web services are being designed and implemented in restful style.  For example, Amazon.com provides close-to-rest-style Web services for book searches, and the Web services provided by Yahoo are restful. Second, Resttemplate is the client that spring provides to access the rest service, Resttemplate provides a variety of convenient ways to access the remote HTTP service, which can greatly improve the efficiency of client writing.  Calling Resttemplate's default constructor, the Resttemplate object creates HTTP requests at the bottom by using implementations under the Java.net package, and can specify different HTTP request methods by using Clienthttprequestfactory. Three, here mainly introduces several Resttemplate usage 1) The first POST request
 PublicResponseentity doPost (String URL, map<string,object>map) {resttemplate resttemplate=Newresttemplate (); Httpheaders httpheaders=Newhttpheaders ();        Httpheaders.setcontenttype (Mediatype.application_json_utf8); Gson Gson=NewGson (); Httpentity<String> httpentity =Newhttpentity (Gson.tojson (map), httpheaders); Responseentity responseentity= Resttemplate.exchange (URL, httpmethod.post, httpentity, Object.class); returnresponseentity; }

Note: This method is mainly used for post data transmission, because the simplicity of rest, in the use of the above will also get hate applications.

2) Second GET request

 Public responseentity doget (String url) {        new  resttemplate ();         New httpheaders ();        Httpheaders.setcontenttype (mediatype.text_html);        HttpentityNew  httpentity (httpheaders);         = Resttemplate.exchange (URL, httpmethod.get, httpentity, Object.  Class);         return responseentity;    }

Description: The GET request does not have much explanation, basically the settings of the face are like this

3) The third from method

  public  responseentity dofrom (String URL, linkedmultivaluemap<string, Object> map) {resttemplate resttemplate  = new<        /span> Resttemplate (); Httpheaders httpheaders  = new   Httpheaders (        );        Httpheaders.setcontenttype (mediatype.application_form_urlencoded); Httpentity  <String> httpentity = new          Httpentity (httpheaders); responseentity responseentity  = Resttemplate.exchange (URL, httpmethod.post, httpentity, Object.class  );     return   responseentity; }

Description: The form of the use of the way although not common, but the application of time also need to pay attention to several points, data form linkedmultivaluemap and HashMap different storage mode is name=rest&password=123.

And the way of Content-type is application/x-www-form-urlencoded. This form of processing, for the processing of data to pay special attention to

Java's resttemplate Access application

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.