Recently use resttemplate send POST request, encountered a lot of problems, such as conversion httpmessage failure, Chinese garbled and so on, adjusted for a long time to find the following more convenient method:
resttemplate resttemplate = new resttemplate (); Httpheaders headers = new Httpheaders (); MediaType type = Mediatype.parsemediatype ("Application/json; Charset=utf-8 "" Accept ", MediaType.APPLICATION_JSON.toString ()); Jsonobject jsonobj = Jsonobject.fromobject (params); Httpentity <String> formentity = new Httpentity<string> (jsonobj.tostring (), headers); String result = Resttemplate.postforobject (URL, formentity, String.);
If you directly use the Postforobject to pass an object into an error that is easily present in the no suitable httpmessageconverter found for request type, it is recommended to go directly to the string first, See Jsonobj.otstring (), online someone said set Resttemplate Httpmessageconverter, tried to introduce a variety of packages. In addition to pay attention to the problem of Chinese coding, some people on the internet said stringhttpmessageconverter default use of iso-8859-1, to be designated as UTF-8 encoding, the attempt did not succeed, and finally by specifying the ContentType way to solve.
Send a POST request using Resttemplate