Hi,
I create a very simple Rest interface for a demo application which looks up either all Employees or just 1 Employee by id.
In the applicationContext.xml I use the ContentNegotiationViewController to return either JSP or XML and I configured the MarshallingView to use a CastorMashaller.Code:package demo.employee.web; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import eu.diversit.demo.employee.model.Employee; @RequestMapping("/rest/employee/**") @Controller public class EmployeeRestController { @RequestMapping(method=RequestMethod.GET) public ModelAndView list() { List<Employee> employees = Employee.findAllEmployees(); ModelAndView mav = new ModelAndView("content"); mav.addObject("employees", employees); return mav; } @RequestMapping(value="{id}", method=RequestMethod.GET) public ModelAndView get(@PathVariable("id") long eid) { Employee e = Employee.findEmployee(eid); ModelAndView mav = new ModelAndView("content"); mav.addObject("employee",e); return mav; } }
When I request all employees, the result is as expected: a list with only 1 employee (which is the only one available):Code:<bean name="content" class="org.springframework.web.servlet.view.xml.MarshallingView"> <constructor-arg> <bean class="org.springframework.oxm.castor.CastorMarshaller"> <property name="mappingLocation" value="classpath:castor-mapping.xml"/> </bean> </constructor-arg> </bean> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="mediaTypes"> <map> <entry key="xml" value="application/xml"/> <entry key="html" value="text/html"/> </map> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </list> </property> </bean>
When I request 1 employee by ID, I get a completely different result. Even though I just add an Employee instance to the ModelAndView object, the result displays a very long BeanPropertyBindingResult instance:Code:<?xml version="1.0" encoding="UTF-8"?> <array-list><employee><phone>4943894279473</phone><version>1</version><birthdate>1901-05-10T00:00:00.000+00:19</birthdate><email>myname@employee.org</email><name>MyName</name><title>Nobody</title><id>1</id></employee></array-list>
Why does this happen?Code:<?xml version="1.0" encoding="UTF-8"?> <bean-property-binding-result field-error-count="0" global-error-count="0" error-count="0"><property-accessor xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com" extract-old-value-for-editor="true" xsi:type="java:org.springframework.beans.BeanWrapperImpl"><root-instance xsi:type="employee"><phone>4943894279473</phone><version>0</version><birthdate>1973-05-10T00:00:00.000+01:00</birthdate><email>jdboer@diversit.eu</email><name>jdboer</name><title>Architect</title><id>1</id></root-instance><nested-path></nested-path><wrapped-instance xsi:type="employee"><phone>4943894279473</phone><version>0</version><birthdate>1973-05-10T00:00:00.000+01:00</birthdate><email>jdboer@diversit.eu</email><name>jdboer</name><title>Architect</title><id>1</id></wrapped-instance><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>birthdate</display-name><name>birthdate</name><short-description>birthdate</short-description></property-descriptors><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>class</display-name><name>class</name><short-description>class</short-description></property-descriptors><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>email</display-name><name>email</name><short-description>email</short-description></property-descriptors><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>id</display-name><name>id</name><short-description>id</short-description></property-descriptors><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>name</display-name><name>name</name><short-description>name</short-description></property-descriptors><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>phone</display-name><name>phone</name><short-description>phone</short-description></property-descriptors><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>title</display-name><name>title</name><short-description>title</short-description></property-descriptors><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>version</display-name><name>version</name><short-description>version</short-description></property-descriptors></property-accessor><property-editor-registry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com" extract-old-value-for-editor="true" xsi:type="java:org.springframework.beans.BeanWrapperImpl"><root-instance xsi:type="employee"><phone>4943894279473</phone><version>0</version><birthdate>1973-05-10T00:00:00.000+01:00</birthdate><email>jdboer@diversit.eu</email><name>jdboer</name><title>Architect</title><id>1</id></root-instance><nested-path></nested-path><wrapped-instance xsi:type="employee"><phone>4943894279473</phone><version>0</version><birthdate>1973-05-10T00:00:00.000+01:00</birthdate><email>jdboer@diversit.eu</email><name>jdboer</name><title>Architect</title><id>1</id></wrapped-instance><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>birthdate</display-name><name>birthdate</name><short-description>birthdate</short-description></property-descriptors><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>class</display-name><name>class</name><short-description>class</short-description></property-descriptors><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>email</display-name><name>email</name><short-description>email</short-description></property-descriptors><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>id</display-name><name>id</name><short-description>id</short-description></property-descriptors><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>name</display-name><name>name</name><short-description>name</short-description></property-descriptors><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>phone</display-name><name>phone</name><short-description>phone</short-description></property-descriptors><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>title</display-name><name>title</name><short-description>title</short-description></property-descriptors><property-descriptors bound="false" constrained="false" expert="false" hidden="false" preferred="false"><display-name>version</display-name><name>version</name><short-description>version</short-description></property-descriptors></property-editor-registry><model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com" xsi:type="java:org.exolab.castor.mapping.MapItem"><key xsi:type="java:java.lang.String">org.springframework.validation.BindingResult.employee</key></model><model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com" xsi:type="java:org.exolab.castor.mapping.MapItem"><key xsi:type="java:java.lang.String">employee</key><value xsi:type="employee"><phone>4943894279473</phone><version>0</version><birthdate>1973-05-10T00:00:00.000+01:00</birthdate><email>jdboer@diversit.eu</email><name>jdboer</name><title>Architect</title><id>1</id></value></model><message-codes-resolver xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:java="http://java.sun.com" xsi:type="java:org.springframework.validation.DefaultMessageCodesResolver"/><target xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="employee"><phone>4943894279473</phone><version>0</version><birthdate>1973-05-10T00:00:00.000+01:00</birthdate><email>jdboer@diversit.eu</email><name>jdboer</name><title>Architect</title><id>1</id></target><nested-path></nested-path><object-name>employee</object-name></bean-property-binding-result>
How can I fix it?
Adding an attribute name to the object which is added to the ModelAndView instance does not make any difference.
Creating a custom castor-mapping also does not make any difference.
It should just return the xml of 1 Employee instance. Any ideas?


Reply With Quote