【发布时间】:2013-08-20 05:30:55
【问题描述】:
默认情况下,我从 spring restful 服务获取 xml 响应。 在我的 Spring RESTful 客户端中使用 RestTemplate 如何将接受媒体类型配置为 JSON?
【问题讨论】:
默认情况下,我从 spring restful 服务获取 xml 响应。 在我的 Spring RESTful 客户端中使用 RestTemplate 如何将接受媒体类型配置为 JSON?
【问题讨论】:
如果 Rest Service 只生成 XML,那么我认为您不能接受它作为 JSON。在这种情况下,您需要在 Rest Service 中将 MediaType 添加为 "application/json" 以及现有的 xml 响应。
例如,在 Spring Restful Service 中,注释将是
@RequestMapping(value = "/myurl", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
如果 Rest Service 正在生成 Json 和 Xml,那么在 rest 客户端中你需要这样做:
ResponseEntity<YourClass> apiResp = restTemplate.exchange(url, HttpMethod.GET, request, YourClass.class);
YourClass output=apiResp.getBody();
【讨论】: