【发布时间】:2017-02-17 08:58:09
【问题描述】:
我正在使用 springframework -RestTemplate, 为了发出 get 请求,并将 xml 响应转换为 java 对象。 运行后:RestTemplate.exchange, 我遇到了以下异常:
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not instantiate value of type [simple type, class Order] from
来自客户端的 XML 响应,我将其添加到标题中:
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
那么为什么它试图将响应解析为 JSON 呢? 我该如何解决?
谢谢!!
【问题讨论】:
-
您可能没有 xml 到对象转换所需的 MessageConverter
-
嗨,我尝试添加:restTemplate.getMessageConverters().add(new MappingJackson2XmlHttpMessageConverter());但仍然得到这个异常。我应该使用哪个 messageConverter?谢谢
-
您是否也将 Jackson2 添加到您的类路径中?
-
我正在使用 mvn 并添加以下依赖项:
com.fasterxml.jackson.core jackson-databind 谢谢
标签: java xml spring resttemplate