在春季添加Jaxb2消息转换器会中断Jackson2 json映射 [英] Adding Jaxb2 message converter in Spring breaks Jackson2 json mapping
问题描述
我正在努力做到这一点,以便我的Spring rest应用程序可以处理xml和json响应,但是似乎添加一个Jaxb消息转换器破坏了我的json映射.
I'm trying to make it so my Spring rest application can handle both xml and json responses, but it seems like adding a Jaxb message converter has broken my json mapping.
@Bean
public MappingJackson2HttpMessageConverter jsonConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(String.class, new StringSerializer());
ObjectMapper mapper = new ObjectMapper()
.registerModule(simpleModule);
converter.setObjectMapper(mapper);
return converter;
}
@Bean
public Jaxb2RootElementHttpMessageConverter jaxbConverter() {
return new Jaxb2RootElementHttpMessageConverter();
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jsonConverter());
converters.add(jaxbConverter());
}
如果我在那里注释掉第二和第三种方法,那么一切都会再次开始工作(当然,除了xml映射!).但是,有了这些内容,我就搞砸了,例如序列化List<String>
会生成[APPLEORANGEBANANA]
,其中苹果,橙和香蕉是列表中的单独字符串.
If I comment out the second and third methods there, everything starts working again (except the xml mapping of course!). With those there, though, I get screwed up stuff, like serializing a List<String>
results in [APPLEORANGEBANANA]
, where apple, orange, and banana are separate strings in the list.
如果我直接使用杰克逊对象映射器映射到json,则不会出现此问题,但是使用@ResponseBody
批注自动序列化为json时,我会遇到此问题.
If I directly use a jackson object mapper to map to json, it doesn't have that issue, but using the @ResponseBody
annotation to automatically serialize to json I have this issue.
有人有什么主意吗?
推荐答案
我删除了configureMessageConverters
,它自动拾取了两个转换器,都没有问题.
I removed the configureMessageConverters
and it picked up both of the converters automatically and neither has an issue.
这篇关于在春季添加Jaxb2消息转换器会中断Jackson2 json映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!