我使用下面的代码来调用一个REST Web API,
WebTarget target = client.target(CommonConfig.getProperty(APIX_SEND_EMAIL_URL));
Response response = target.request("application/json").accept(MediaType.APPLICATION_JSON_TYPE).header("Authorization", token)
.post(Entity.json(email));
现在我想使用Spring Boot RestTemplate来调用这个Web服务,我正在使用下面的代码:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
List<MediaType> mediaList = new ArrayList<MediaType>();
mediaList.add(MediaType.APPLICATION_JSON);
mediaList.add(MediaType.TEXT_PLAIN);
headers.setAccept(mediaList);
//Json is the json object of the request data
HttpEntity<String> entity = new HttpEntity<String>(json.toString(), headers);
ResponseEntity<String> loginResponse = restTemplate
.exchange(url, HttpMethod.POST, entity, String.class);
但是我收到错误 415 Unsupported Media Type 。任何人都可以帮助我理解错误的原因,我如何使用RestTemplate来解决它?
答案 0 :(得分:0)
为什么不使用RestTemplate的postForObject()方法?
SomeObject json = SomeObject();
SomeOtherObject response = restTemplate.postForObject("url", json , SomeOtherObject.class);