Note: Spring Cloud Ribbon resttemplate Detailed

Source: Internet
Author: User

Detailed Introduction Resttemplate The implementation of the service invocation for several different request types and parameter types, in the sample code resttemplate It's all through spring . injection mode, the relevant code is as follows:

@Autowired

Private resttemplate resttemplate;

??

in the application main class need to increase Bean, the code is as follows:

@LoadBalanced

@Bean

Public resttemplate createresttemplate() {

???????? return newresttemplate();

}

  • GET Request

    in the resttemplate , a GET request can be called in the following way:

    • getforentitymethod: The method returns theresponseentity, the object is springtheHTTPthe encapsulation of the request response object, which mainly stores theHTTPa few important elements, such asHTTPenumeration object for request StateHttpstatus, in his father's classhttpentityis also stored in theHTTPthe header information Objecthttpheadersas well as the generic type of the request body object, for example, the following code accesses the service's/Getrequest, because the second parameter is aString.classso the returnedresponseentityobject in theBodythe content type is converted to a string return:

    ???????????? responseentity<string> responseentity = null;

???????????????????????? Responseentity =

???????????????????????????????????????? Resttemplate.getforentity ("Http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/hello/get, String.class);

???????????????????????? if (responseentity.getstatuscode () = = Httpstatus.ok) {

???????????????????????????????? return responseentity.getbody ();

????????????????????????}

???????????????????????? return"response status" + Responseentity.getstatuscodevalue ();

    • Getforobject method: This method can be understood as the getforentity further encapsulation, through Httpmessageconverterextractor the HTTP Request Response Body Body nie rong Nyainrong The object transformation, implementing the request to return the wrapped object content directly:

      ?? String BODY =

      ?????????????????? Resttemplate.getforobject ("Http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/hello/get", String.class);

  • POST Request

    in Resttemplate , the POST requests can be implemented by invoking the following three methods.

      • postforentity method: The method is the same as The getforentity in the GET request is similar, returning responseentity<T> objects after the call , where T for the request response Body type, the sample code is as follows:

    ???????????? User user = newuser("Didi", +);

    ???????????? Responseentity<string> responseentity =

    ???????????? resttemplate.postforentity ("Http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/hello/post", user,string.class);

    ???????????? String BODY = Responseentity.getbody ();

    Here's what to note is the newly added request parameter, this parameter can be a normal object, or it can be a httpentity object, if it is a normal object, Resttemplate httpentity object to process and treat the object as complete body httpentity object, then it will be treated as a full HTTP Request object to handle, this object not only contains the body header content, the sample code is as follows:

    Responseentity<string> responseentity = null;

    Httpheaders headers = newhttpheaders();

    list<mediatype> accept = new linkedlist<> ();

    Accept.add (Mediatype.application_json_utf8);

    Headers.setaccept (accept);

    Headers.setcontenttype (mediatype.application_form_urlencoded);

    ??

    multivaluemap<string, string> postparameters = new linkedmultivaluemap<> ();

    Postparameters.add ("id", "one");

    Postparameters.add ("name", "AoA");

    Postparameters.add ("comment", " " ");

    ??

    httpentity<multivaluemap<string, string>> requestentity = new httpentity<> (postParameters , headers);

    ??

    Responseentity =

    ?????????????? Resttemplate.postforentity ("Http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/webapi/hello/post", requestentity, String.class);

    String BODY = Responseentity.getbody ();

    ??

      • Postforobject methods: The method was followed Getforobject similar to the type, his use is to simplify postforentity by responding directly to the request. Body the content is packaged as an object to return to use, with the sample code as follows:

    ???????????? User user = newuser("Didi", +);

    ???????????? String BODY =

    ??????????? ? resttemplate. Postforobject ("Http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/hello/post", user,string.class);

      • postforlocation Method: This method implements the post request a resource and return the resource's uri, the uri is equivalent to specifying a return type, so this method implements the post request does not need to be like postforentity postforobject as specified responsetype, the sample code is as follows:

    ???????????? User user = newuser("Didi", +);

    ???????????? URI ResponseUri =

    Resttemplate.postforlocation ("Http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/hello/post", user);

  • PUT Request

    in the resttemplate , the put request it to return as void type, there is no return content, so there is no other function-defined Responsetype parameters, in addition to other incoming parameter definitions and usage with Postforobject Basically, the sample code is as follows:

    map<string, string> map = new hashmap<> ();

    Map.put ("Put1", "23434");

    Map.put ("Put2", "3544545");

    ?????????????????

    Resttemplate.put ("Http://ORG. Drsoft. WEBSERVICE. Helloservice/webapi/hello/put/{0}/{1} ", Map, 123213, " Sunderland vice Ke ji ");

  • Delete Request

    in the resttemplate , the Delete requests can be implemented through the delete method, consistent with the put request, with a return type of void type, so the delete The request does not need to return data, the sample code is as follows:

    ? Integer id = + ;

    ? Resttemplate.delete ("Http://org.drsoft.webservice.helloservice/hello/delete?id={1}", id);

Notes: Spring Cloud Ribbon resttemplate detailed

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.