Spring Jaxb2RootElementHttpMessageConverter 不适用于 jaxb 注释

eph*_*der 3 java xml spring spring-mvc jaxb

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

基本响应:

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

添加成员响应:

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的代码中,我必须将 xml 保存到数据库中,因此我必须手动将其转换为稍后保存的字符串,为此我使用 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;
}
Run Code Online (Sandbox Code Playgroud)

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

<response>
    <status>500</status>
</response> 
Run Code Online (Sandbox Code Playgroud)

但它返回这个作为 POSTMAN 中的响应:

<AddMemberResponse>
    <status>500</status>
    <message/>
    <confirmationCode>UVHRWLHB6UMQ</confirmationCode>
    <member/>
</AddMemberResponse>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

更新1:

as "application/xml" using [org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter@3f767117]
Run Code Online (Sandbox Code Playgroud)

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

eph*_*der 5

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

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-xml-provider</artifactId>
    <version>2.4.3</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

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