繁体   English   中英

在Spring中添加Jaxb2消息转换器会破坏Jackson2 json映射

[英]Adding Jaxb2 message converter in Spring breaks Jackson2 json mapping

我正在尝试这样做,所以我的Spring rest应用程序可以处理xml和json响应,但似乎添加Jaxb消息转换器已经破坏了我的json映射。

@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映射!)。 然而,在那里,我搞砸了一些东西,比如在[APPLEORANGEBANANA]序列化List<String>结果,其中apple,orange和banana是列表中的单独字符串。

如果我直接使用jackson对象映射器映射到json,它没有那个问题,但是使用@ResponseBody注释自动序列化为json我有这个问题。

有人有主意吗?

这就是我做到的。

@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
}

并在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.MappingJackson2HttpMessageConverter">
 </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>

我删除了configureMessageConverters ,它自动拾取了两个转换器,但都没有问题。

事情发生在我身上。 两次:-)

在一种情况下,在添加我的转换器之前简单的converters.clear()解决了这个问题。 看起来Spring默认添加了一些转换器。 使用XML注入它们会删除Spring隐式设置的那个,而从代码中显式添加它们则不会。

在另一种情况下,问题是在请求中设置了正确的标头,即content-typeaccept 在请求映射中,我必须指定“消耗”和“生成”参数,分别映射两个头。

public class MyRequestHandler {
     ...

     @RequestMapping(... , consumes={ MediaType.APPLICATION_JSON_VALUE, produces = { MediaType.APPLICATION_JSON_VALUE } }
     public ResponseEntity<MyResultClass> doSomething(...) { ... }

     ...
}

这解决了我的问题。

当您覆盖configureMessageConverters时,它不会添加默认消息转换器。 试试下面的代码。

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(jsonConverter());
    converters.add(jaxbConverter());
    super.addDefaultHttpMessageConverters();
}

暂无
暂无

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

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