我将 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;
}
}
添加成员响应:
@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 保存到数据库中,所以我必须手动将其转换为一个字符串以便稍后保存,为此我使用了 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>
更新 1:
as "application/xml" using [org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter@3f767117]
显示在日志中,因此在为响应编码(marshal)它时没有使用正确的转换器。
最佳答案
我必须删除对类路径的 jackson 依赖:
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-xml-provider</artifactId>
<version>2.4.3</version>
</dependency>
MappingJackson2XmlHttpMessageConverter@3f767117 优先于 jaxb 转换器,如果它在类路径上,因此编码(marshal)处理是由该转换器而不是我的 jaxb 完成的。
关于java - Spring Jaxb2RootElementHttpMessageConverter 不使用 jaxb 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34995549/