Spring Jaxb2RootElementHttpMessageConverter不使用jaxb注释

时间:2016-01-25 14:55:22

标签: java xml spring spring-mvc jaxb

我有spring设置使用:Jaxb2RootElementHttpMessageConverter作为将对象转换为xml的消息转换器。在我的休息服务中,当生成响应时,它不符合我在模型类上提供的JAXB注释。

BaseResponse:

@XmlRootElement(name="response")
@XmlSeeAlso({AddMemberResponse.class,UpdateMemberResponse.class})
public abstract class BaseResponse {


    private int status;
    private String message;
    private String confirmationCode;

    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public int getStatus() {
        return status;
    }
    public void setStatus(int httpInternalError) {
        this.status = httpInternalError;
    }
    public String getConfirmationCode() {
        return confirmationCode;
    }
    public void setConfirmationCode(String confirmationCode) {
        this.confirmationCode = confirmationCode;
    }
}

AddMemberResponse:

@XmlRootElement(name = "response")
@XmlAccessorType(XmlAccessType.FIELD)
public class AddMemberResponse extends BaseResponse {

    private Member member;

    public AddMemberResponse() {
        super();
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }
}

在我的代码中的一点,我必须将xml保存到数据库,所以我必须手动将其转换为稍后要保存的String,为此我使用JaxbMarshaller类,这很好用:

/**
 * Converts object to xml using jaxb marshaller
 */
private String objectToXML(Object graph) throws IOException {

    String finalstring = null;
    try {

        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setPackagesToScan("com.company.ws");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("jaxb.formatted.output", true);
        marshaller.setMarshallerProperties(map);

        // create a StringWriter for the output
        StringWriter outWriter = new StringWriter();
        StreamResult result = new StreamResult(outWriter);
        marshaller.marshal(graph, result);

        StringBuffer sb = outWriter.getBuffer();
        finalstring = sb.toString();
        log.debug(finalstring);

    } catch(Exception e) {
        e.printStackTrace();
    }
    return finalstring;
}

它使用jaxbmarshaller将其存储在数据库中,如下所示:

<response>
    <status>500</status>
</response> 

但它将此作为POSTMAN中的响应返回:

<AddMemberResponse>
    <status>500</status>
    <message/>
    <confirmationCode>UVHRWLHB6UMQ</confirmationCode>
    <member/>
</AddMemberResponse>

500来自它无法连接到外部服务的数据,所以它不相关,它应该仍然返回所描述的初始方式。

配置了消息转换器的app-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- Enables the Spring4 @Controller  -->
    <mvc:annotation-driven />

    <!-- To  convert JSON to Object and vice versa -->
    <bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </bean> 

     <!-- To  convert XML to Object and vice versa -->
    <bean id="xmlMessageConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter">
    </bean> 


<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter"/>
            <ref bean="xmlMessageConverter"/>
        </list>
    </property> 
</bean>     
    <!-- Dozer configuration -->
    <bean id="beanMapper" class="org.dozer.DozerBeanMapper">
      <property name="mappingFiles">
        <list>
          <value>dozerMapping.xml</value>
        </list>
      </property>
    </bean>


    <context:component-scan base-package="com.company" />

</beans>

UPDATE1:

as "application/xml" using [org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter@3f767117]

显示在日志中,因此在编组响应时它没有使用正确的转换器。

1 个答案:

答案 0 :(得分:2)

我必须删除类路径上的jackson依赖项:

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-xml-provider</artifactId>
    <version>2.4.3</version>
</dependency>

MappingJackson2XmlHttpMessageConverter @ 3f767117优先于jaxb转换器,如果它在类路径上,因此编组是由转换器完成的,而不是我的jaxb转换器。