在使用RestTemplate时,我们如何configuration内部的Jackson映射器?
我想更新Spring RestTemplate使用的jackson映射器的SerializationConfig.Feature …属性,任何想法如何我可以到它或我可以/应该configuration它。
默认的RestTemplate
构造函数注册一组HttpMessageConverter
:
this.messageConverters.add(new ByteArrayHttpMessageConverter()); this.messageConverters.add(new StringHttpMessageConverter()); this.messageConverters.add(new ResourceHttpMessageConverter()); this.messageConverters.add(new SourceHttpMessageConverter()); this.messageConverters.add(new XmlAwareFormHttpMessageConverter()); if (jaxb2Present) { this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter()); } if (jacksonPresent) { this.messageConverters.add(new MappingJacksonHttpMessageConverter()); } if (romePresent) { this.messageConverters.add(new AtomFeedHttpMessageConverter()); this.messageConverters.add(new RssChannelHttpMessageConverter()); }
MappingJacksonHttpMessageConverter
创buildObjectMapper
实例。 你可以find这个转换器并且replaceObjectMapper
或者在它之前注册一个新的。 这应该工作:
@Bean public RestOperations restOperations() { RestTemplate rest = new RestTemplate(); //this is crucial! rest.getMessageConverters().add(0, mappingJacksonHttpMessageConverter()); return rest; } @Bean public MappingJacksonHttpMessageConverter mappingJacksonHttpMessageConverter() { MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter(); converter.setObjectMapper(myObjectMapper()); return converter; } @Bean public ObjectMapper myObjectMapper() { //your custom ObjectMapper here }
在XML中是这样的:
<bean id="restOperations" class="org.springframework.web.client.RestTemplate"> <property name="messageConverters"> <util:list> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/> <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/> <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/> <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="objectMapper" ref="customObjectMapper"/> </bean> </util:list> </property> </bean> <bean id="customObjectMapper" class="org.codehaus.jackson.map.ObjectMapper"/>
请注意,转换并不是真正的1:1 – 我必须使用@Configuration
方法显式创buildmessageConverters
列表,而我可以引用现有的并简单地修改它。 但是,这应该工作。
如果你不使用Spring IOC,你可以这样做(Java 8):
ObjectMapper objectMapper = new ObjectMapper(); // configure your ObjectMapper here RestTemplate restTemplate = new RestTemplate(); MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter(); messageConverter.setPrettyPrint(false); messageConverter.setObjectMapper(objectMapper); restTemplate.getMessageConverters().removeIf(m -> m.getClass().getName().equals(MappingJackson2HttpMessageConverter.class.getName())); restTemplate.getMessageConverters().add(messageConverter);
RestTemplate将初始化其默认消息转换器。 你应该用你自己的beanreplaceMappingJackson2HttpMessageConverter
,它应该使用你想要使用的对象映射器。 这对我工作:
@Bean public RestTemplate restTemplate() { final RestTemplate restTemplate = new RestTemplate(); //find and replace Jackson message converter with our own for (int i = 0; i < restTemplate.getMessageConverters().size(); i++) { final HttpMessageConverter<?> httpMessageConverter = restTemplate.getMessageConverters().get(i); if (httpMessageConverter instanceof MappingJackson2HttpMessageConverter){ restTemplate.getMessageConverters().set(i, mappingJackson2HttpMessageConverter); } } return restTemplate; } @Bean public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); converter.setObjectMapper(myObjectMapper()); return converter; } @Bean public ObjectMapper myObjectMapper() { // return your own object mapper }
为了完成其他的答案:如果你的ObjectMapper
只注册一个带有自定义序列化器/反序列化器的Jackson Module
,你可能想直接在RestTemplate
的默认MappingJackson2HttpMessageConverter
的现有ObjectMapper
上注册你的模块,如下所示(没有DI的例子,但是如果使用DI):
SimpleModule module = new SimpleModule(); module.addSerializer(...); module.addDeserializer(...); MappingJackson2HttpMessageConverter messageConverter = restTemplate.getMessageConverters().stream() .filter(MappingJackson2HttpMessageConverter.class::isInstance) .map(MappingJackson2HttpMessageConverter.class::cast) .findFirst().orElseThrow( () -> new RuntimeException("MappingJackson2HttpMessageConverter not found")); messageConverter.getObjectMapper().registerModule(module);
这将允许您完成原始ObjectMapper
的configuration(由Spring的Jackson2ObjectMapperBuilder
),而不是replace它。