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);

 

posted on 2018-10-11 18:17  wanbao  阅读(1779)  评论(0编辑  收藏  举报