Recently usedRestTemplate Send outpost request, A lot of problems, ConversionhttpMessage fail, Chinese random code, etc, It took a long time to find the following easier way:

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("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.class);

as Fruit is directly used inpostForObject It's easy to pass in objects inno suitable HttpMessageConverter found for request type Mistake, It is recommended to convert to string directly, seejsonObj.otString(), Someone on the Internet said that settingRestTemplate OfHttpMessageConverter, Try to introduce all kinds of bags. In addition, we should pay attention to the problem of Chinese coding, Someone on the Internet saidStringHttpMessageConverter Default useISO-8859-1, Designated asUTF-8 Code, I didn't succeed in my own attempt, Finally, by specifyingcontentType It's solved.