简体   繁体   English

发布时Spring RestTemplate消息转换器优先级

[英]Spring RestTemplate message converter priority when posting

What is the most convenient way to influence the priority of the message converters Spring applies when POSTing with RestTemplate ? 什么是影响消息转换器优先级的最方便的方法Spring在使用RestTemplate时适用?

Use case: I want to ensure a given entity is POSTed as JSON rather than eg XML when I do restTemplate.postForEntity(url, entity, Void.class) . 使用案例:当我执行restTemplate.postForEntity(url, entity, Void.class)我想确保给定的实体以JSON而不是例如XML进行restTemplate.postForEntity(url, entity, Void.class)

Default 默认

By default the entity is converted to XML because the MappingJackson2XmlHttpMessageConverter takes precedence over the MappingJackson2HttpMessageConverter . 默认情况下,实体将转换为XML,因为MappingJackson2XmlHttpMessageConverter优先于MappingJackson2HttpMessageConverter The default list of converters for my app appears to be (Spring scans the classpath to see what's available): 我的应用程序的默认转换器列表似乎是(Spring扫描类路径以查看可用的内容): 在此输入图像描述

Option 1 选项1

You can configure the message converters explicitly for a given RestTemplate instance like so restTemplate.setMessageConverters(Lists.newArrayList(new MappingJackson2HttpMessageConverter())) . 您可以为给定的RestTemplate实例显式配置消息转换器,如restTemplate.setMessageConverters(Lists.newArrayList(new MappingJackson2HttpMessageConverter())) This is inconvenient if the instance is shared (as a Spring bean for example) as you might need converter X in one case and converter Y in a different one. 如果实例是共享的(例如Spring bean),这是不方便的,因为在一种情况下可能需要转换器X而在另一种情况下可能需要转换器Y.

Option 2 选项2

You can set Accept and Content-Type HTTP headers explicitly in which case Spring will use a matching message converter. 您可以显式设置AcceptContent-Type HTTP标头,在这种情况下,Spring将使用匹配的消息转换器。 The downside is that you have to resort to RestTemplate.exchange instead of RestTemplate.postForEntity which means: extra code, less convenience. 缺点是您必须使用RestTemplate.exchange而不是RestTemplate.postForEntity ,这意味着:额外的代码,更少的便利。

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity requestEntity = new HttpEntity(entity, requestHeaders);
restTemplate.exchange(url, HttpMethod.POST, requestEntity, Void.class);

Option 3 选项3

This might be what I'm looking for :) 这可能是我正在寻找的:)

According to the Spring javadoc ( https://docs.spring.io/spring-framework/docs/current/javadoc-api/index.html?org/springframework/web/client/RestTemplate.html ) you can still use postForEntity, 根据Spring javadoc( https://docs.spring.io/spring-framework/docs/current/javadoc-api/index.html?org/springframework/web/client/RestTemplate.html ),你仍然可以使用postForEntity,

public <T> ResponseEntity<T> postForEntity(java.lang.String url,
                                       @Nullable
                                       java.lang.Object request,
                                       java.lang.Class<T> responseType,
                                       java.util.Map<java.lang.String,?> uriVariables)
                                throws RestClientException
....

The request parameter can be a HttpEntity in order to add additional HTTP headers to the request. request参数可以是HttpEntity ,以便为请求添加额外的HTTP标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM