POST Requestin 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 Requestin 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 ");