我已经设置了Spring来使用Jaxb2RootElementHttpMessageConverter作为消息转换器将对象转换为XML。在我的REST服务中,当响应生成时,它不遵循我在模型类上提供的JAXB注释。
:
它使用jaxbmarshaller将此内容存储在数据库中,方法如下:
但是它在POSTMAN中返回这个响应:
:
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保存到数据库的地方,因此我需要手动将其转换为字符串以便稍后保存,为此我使用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]
在日志中显示,因此在将其编组为响应时未使用正确的转换器。