restTemplate getForEntity 收到 xml

Eureka 2.2.3 后,似乎增加了 restTemplate 对 xml 的支持,并且在 restTemplate.getForEntity 时默认将接收到的 json 转为 xml

可以使用 exchange 并指定 reponseType

HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<Object> entityForGet = new HttpEntity<>(null, headers);

restTemplate.exchange(url, HttpMethod.GET, entityForGet, User.class).getBody();

你可能感兴趣的:(Java,Spring)