Send a POST request using Resttemplate

Source: Internet
Author: User

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

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.