Java 在 Spring 中添加 Jaxb2 消息转换器会破坏 Jackson2 json 映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18650336/
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
Adding Jaxb2 message converter in Spring breaks Hymanson2 json mapping
提问by CorayThan
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.
我正在努力使我的 Spring rest 应用程序可以处理 xml 和 json 响应,但似乎添加一个 Jaxb 消息转换器破坏了我的 json 映射。
@Bean
public MappingHymanson2HttpMessageConverter jsonConverter() {
MappingHymanson2HttpMessageConverter converter = new MappingHymanson2HttpMessageConverter();
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());
}
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.
如果我在那里注释掉第二个和第三个方法,一切都会重新开始工作(当然除了 xml 映射!)。但是,在那里,我会搞砸一些事情,例如将List<String>
结果序列化为[APPLEORANGEBANANA]
,其中苹果、橙子和香蕉在列表中是单独的字符串。
If I directly use a Hymanson 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.
如果我直接使用 Hymanson 对象映射器映射到 json,它没有那个问题,但是使用@ResponseBody
注释自动序列化为 json 我有这个问题。
Anyone have any ideas?
谁有想法?
采纳答案by CorayThan
I removed the configureMessageConverters
and it picked up both of the converters automatically and neither has an issue.
我删除了configureMessageConverters
它,它自动拿起了两个转换器,都没有问题。
回答by spal
This is how I did it.
我就是这样做的。
@RequestMapping(method = RequestMethod.GET, value = "/accounts/{accountId}", produces = {APPLICATION_XML_VALUE, APPLICATION_JSON_VALUE})
@ResponseBody
@ResponseStatus(value = HttpStatus.OK)
public Account getAccount(@PathVariable String accountId) {
return new Account(); // populate Account VO and send
}
and in the XML file
并在 XML 文件中
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" >
<mvc:message-converters register-defaults="false">
<ref bean="xmlConverter"/>
<ref bean="jsonConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- XML MessageConverter -->
<bean id="xmlConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg ref="jaxbMarshaller"/>
<property name="supportedMediaTypes" value="application/xml" />
</bean>
<!-- JSON MessageConverter -->
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter">
</bean>
<!-- JAXB Classes to be marshalled -->
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="packagesToScan">
<list>
<value>com.test.*</value>
<value>com.*.test</value>
</list>
</property>
</bean>
回答by Stefano Cazzola
Happened the very same to me. Twice :-)
发生在我身上的情况非常相似。两次:-)
In one case, a simple converters.clear()
before adding my converters solved the problem. Looks like Spring adds some converters by default. using XML to inject them deletes the one implicitly set by Spring, while adding them explicitly from code doesn't.
在一种情况下,converters.clear()
添加转换器之前的简单操作解决了该问题。看起来 Spring 默认添加了一些转换器。使用 XML 注入它们会删除 Spring 隐式设置的那些,而从代码中显式添加它们不会。
In the other case the problem was that the correct headers had be set in the request, that is the content-type
and accept
. In the request mappings, then, I had to specify the "consumes" and "produces" parameters, mapping respectively the two headers.
在另一种情况下,问题是在请求中设置了正确的标头,即content-type
and accept
。然后,在请求映射中,我必须指定“consumes”和“produces”参数,分别映射两个标头。
public class MyRequestHandler {
...
@RequestMapping(... , consumes={ MediaType.APPLICATION_JSON_VALUE, produces = { MediaType.APPLICATION_JSON_VALUE } }
public ResponseEntity<MyResultClass> doSomething(...) { ... }
...
}
This solved the problem for me.
这为我解决了这个问题。
回答by c.sankhala
When you override configureMessageConverters, it will not add Default Message Converters. Try below code.
当您覆盖 configureMessageConverters 时,它不会添加默认消息转换器。试试下面的代码。
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jsonConverter());
converters.add(jaxbConverter());
super.addDefaultHttpMessageConverters();
}