java 强制 Spring RestTemplate 使用 XmlConverter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13038529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:17:10  来源:igfitidea点击:

Force Spring RestTemplate to use XmlConverter

javaxmlspringresttemplate

提问by Nathanial

We are integrating with a third party that is sending xml with the content-type header as text/html. We were planning on using Spring's RestTemplate to map it to classes we've generated from xsds, but the RestTemplate fails to find an appropriate converter to use for the content. The third party refuses to fix the content-type because it might break other partner's integration.

我们正在与第三方集成,该第三方将带有内容类型标头的 xml 发送为 text/html。我们计划使用 Spring 的 RestTemplate 将其映射到我们从 xsds 生成的类,但是 RestTemplate 未能找到合适的转换器来用于内容。第三方拒绝修复内容类型,因为它可能会破坏其他合作伙伴的集成。

Is there a way with Spring's RestTemplate to force it to use a specific converter? We are basically just doing the following:

Spring 的 RestTemplate 有没有办法强制它使用特定的转换器?我们基本上只是在做以下事情:

RestTemplate restTemplate = new RestTemplate();
XmlClass xmlClass = restTemplate.getForObject("http://example.com/", XmlClass.class);

And get the following exception:

并得到以下异常:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [XmlClass] and content type [text/html;charset=ISO-8859-1] at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:84)

org.springframework.web.client.RestClientException:无法提取响应:在 org.springframework.web.client 找不到适合响应类型 [XmlClass] 和内容类型 [text/html;charset=ISO-8859-1] 的 HttpMessageConverter。 HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:84)

回答by Nathanial

The solution we implemented was to add a Jaxb2RootElementHttpMessageConverterwith MediaType.TEXT_HTMLto the RestTemplateHttpMessageConverters. It's not ideal since it creates a redundant jaxb message converter but it works.

我们实现的解决方案是增加一个Jaxb2RootElementHttpMessageConverterMediaType.TEXT_HTMLRestTemplateHttpMessageConverters。这并不理想,因为它创建了一个冗余的 jaxb 消息转换器,但它可以工作。

RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
Jaxb2RootElementHttpMessageConverter jaxbMessageConverter = new Jaxb2RootElementHttpMessageConverter();
List<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaType.TEXT_HTML);
jaxbMessageConverter.setSupportedMediaTypes(mediaTypes);
messageConverters.add(jaxbMessageConverter);
restTemplate.setMessageConverters(messageConverters);

回答by Thomas Ehardt

I did not see an example posted of how to actually do this with a custom interceptor, so here is one for reference sake:

我没有看到有关如何使用自定义拦截器实际执行此操作的示例,因此这里有一个供参考:

public class MyXmlInterceptor implements ClientHttpRequestInterceptor {

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
    ClientHttpResponse response = execution.execute(request, body);
    HttpHeaders headers = response.getHeaders();

    // you'd want to check if the value needs to be changed
    if (headers.containsKey("Content-Type")) {
        headers.remove("Content-Type");
    }

    headers.add("Content-Type", "application/xml");

    return response;
}

Then, you would need to add the interceptor to your RestTemplate object:

然后,您需要将拦截器添加到您的 RestTemplate 对象:

RestTemplate t = new RestTemplate();
t.getInterceptors().add(new MyXmlInterceptor());

回答by artbristol

Can you change the content-type header before unmarshalling happens, by adding a custom interceptor http://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/http/client/ClientHttpRequestInterceptor.html?

您可以通过添加自定义拦截器http://static.springsource.org/spring/docs/current/javadoc-api/org/springframework/http/client/ClientHttpRequestInterceptor.html在解组发生之前更改内容类型标头吗?