Like you have noted, what is happening is since you have not specified the explicit model key that needs to be serialized, it is serializing the first non-null valued model object, which in this happens be BindingResult
(used for keeping the binding/validation errors in your model). There are a few fixes that you can make:
a. Specify the exact modelKey
for your marshalling view, this should work and set the model to the specific model key:
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller">
...
</property>
<property name="modelKey" value="command"/>
</bean>
model.addAttribute("command", mymodel);
b. A better fix, IMHO could be to use the http converters in Spring, this way you can return your object from a request mapped method, annotate it with @ResponseBody
and Spring will take care of converting the object to a wire reprsentation(xml or json etc), you will just have to register the correct converter:
@RequestMapping(...)
public @ResponseBody User myMethod(Model model){
return user;
}
<mvc:annotation-driven conversion-service="conversionService">
<mvc:message-converters register-defaults="false"> <!-- you may have to explicitly register other converters though-->
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller">
<bean class="org.springframework.oxm.xstream.XStreamMarshaller"/>
</property>
</bean>
</mvc:message-converters>