RestTemplate设置MediaType
String url = "http://testurl";
Map<String, Object> requestMap = new HashMap<>(); resultMap.put("param1", "value1"); resultMap.put("param2", "value2"); HttpHeaders headers = new HttpHeaders(); MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); headers.setContentType(type); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); HttpEntity<String> formEntity = new HttpEntity<String>(JSONObject.toJSONString(requestMap), headers);
RestTemplate restTemplate = new RestTemplate(); String result = restTemplate.postForObject(url, formEntity, String.class);