When a bean has @XmlElement(name = "someName") the name is correctly used when marshaling the bean to to XML, but the field is not read correctly when unmarshaled.
Simple bean
Simple controllerPHP Code:package com.example;
import javax.xml.bind.annotation.*;
@XmlRootElement()
@XmlAccessorType(XmlAccessType.FIELD)
public class MyData {
@XmlElement(name = "someName") // this doesn't unmarshal correctly
public String aString;
public MyData() {
}
public MyData(String aString) {
this.aString = aString;
}
}
DispatchServlet is mapped in web.xml, and the servlets application context looks like thisPHP Code:package com.example;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
public class TestController {
// -----------------------------------------------------------------
// Instance fields
// -----------------------------------------------------------------
private MyData _data = new MyData("test");
// -----------------------------------------------------------------
// Instance methods
// -----------------------------------------------------------------
@RequestMapping(value = "/get", method = RequestMethod.GET)
@ResponseBody
public MyData get() {
return _data;
}
@RequestMapping(value = "/post", method = RequestMethod.POST)
@ResponseBody
public MyData updateComplex(@RequestBody MyData c) {
_data = c; // c.aString is always null :(
return _data;
}
}
Everything works great if MyData's aString field is not annotated with @XmlElement, MyData objects are correctly marshaled and unmarshaled from java to XML in /get and from XML to java in /post.HTML Code:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"> <context:component-scan base-package="com.example"/> <!-- This configures a chain of converters used for the @PathVariable, @RequestBody and other annotations that controls how data is converted to method arguments in MVC Controller methods --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <util:list id="beanList"> <ref bean="stringHttpMessageConverter"/> <ref bean="JaxbMessageConverter"/> </util:list> </property> </bean> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean id="JaxbMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> <property name="marshaller" ref="jaxbMarshaller"/> <property name="unmarshaller" ref="jaxbMarshaller"/> </bean> <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <property name="classesToBeBound"> <list> <value>com.example.MyData</value> </list> </property> </bean> </beans>
Does anyone know why adding @XmlElement breaks the marshaling, or do am I missing some configuration ?


Reply With Quote
