我无法连接所有允许从 ajax 客户端发送的 xml 被解组为 Java 对象的部分。我将 Jquery 用于 ajax 客户端、Spring3 和 JAXB。以下是关键组件:
Ajax 客户端
function checkForNewActivities(activities) {
$j.ajax({
url: "upload/checkForNewActivities",
type: "POST",
async: true,
dataType: "xml",
data: ({activities:activities}),
contentType: "application/xml",
beforeSend: function() {
alert("sending ajax");
},
complete: function() {
alert("ajax sent");
},
success: function(data) {
alert("success");
},
error: function(jqXHR, textStatus, errorThrown) {
alert(textStatus + errorThrown);
}
});
}
弹簧配置
<bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxbMarshaller" />
<property name="unmarshaller" ref="jaxbMarshaller" />
<property name="supportedMediaTypes" value="application/xml"/>
</bean>
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
a bunch of java classes
</list>
</property>
</bean>
这是作为目标的 Spring Controller 方法:
@RequestMapping(value="/checkForNewActivities",headers="application/xml", method=RequestMethod.POST)
public @ResponseBody String uploadMultipleWorkouts(@RequestBody String activities) {
System.out.println(activities);
return "";
}
使用 RequestMapping 中的 headers="application/xml",这个方法永远不会被调用。如果我删除标头参数,则调用该方法并将活动转储到控制台显示转义的 xml。
我显然错过了如何将此方法连接到 spring 配置,以便进行 xml 解组。