Creating XML view with XStreamMarshaller Spring

I am using spring framework 3.1 (with hibernate) and I am trying to create an XML view like:

<user>
<iduser>1</iduser>
<email>bla@hello.com</email>
<firstName>bob</firstName>
</user>

      

from this java class:

@Entity
public class User {

    @GenericGenerator(name = "table-hilo-generator", strategy = "org.hibernate.id.IncrementGenerator")
    @GeneratedValue(generator = "table-hilo-generator")
    @Id
    @Column(name = "iduser", unique = true, nullable = false)
    private int iduser;

    @NotBlank
    @NotNull
    @NotEmpty
    @Length(max = EMAIL_MAX_SIZE)
    @Column(name = "email", nullable = false)
    private String email;

    @NotBlank
    @NotNull
    @NotEmpty
    @Length(max = FIRST_NAME_MAX_SIZE)
    @Column(name = "firstName", nullable = false)
    private String firstName;
}

      

code> my servlet-conf.xml contains this view in ContentNegotiationViewResolver:

<!-- XML View -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
  <constructor-arg>
    <bean class="org.springframework.oxm.xstream.XStreamMarshaller">
        <property name="aliases">
        <map>
                <entry key="user123" value="com.....entities.User" />
        </map>
        </property>
    </bean>
   </constructor-arg>
</bean>

      

But I don't understand why the result is a weird xml with hundreds of elements that:

<org.springframework.validation.BeanPropertyBindingResult>
<nestedPath/>
<nestedPathStack serialization="custom">
<unserializable-parents/>
<vector>
<default>
<capacityIncrement>0</capacityIncrement>
<elementCount>0</elementCount>
<elementData>
<null/>
<null/>
<null/>
<null/>
<null/>
<null/>
<null/>
<null/>
<null/>
<null/>
</elementData>
</default>
</vector>
</nestedPathStack>
<objectName>user</objectName>
<messageCodesResolver class="org.springframework.validation.DefaultMessageCodesResolver">
<prefix/>

      

1-The marshaller is probably playing too much with reflection, how can I get the expected result that I want? (2nd I am also interested in creating an XML file with a list of users) How can I do this?

+3


source to share


2 answers


As you noted, what happens is that you did not specify an explicit model key to be serialized, it serializes the first nullable object which in this case will be BindingResult

(used to keep the binding / validation errors in your model). There are several fixes you can make:

and. Specify the exact one modelKey

for your marshalling view, this should work and set the model to a 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. Better solution, IMHO might be using http converters in Spring, this way you can return your object from the request mapping method, annotate it with @ResponseBody

and Spring, will take care of converting the object to wire (xml or json etc.), you you just need 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>

      

0


source


I had the same error with XStream and resolved it by applying the following change:

Instead of adding modelKey, you can add the "supportedClasses" property to the Marshaller bean:


<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
    <property name="autodetectAnnotations" value="true"/>
    <property name="supportedClasses">
        <list>
            <value>com.rest.example.model.User</value>
        </list>
    </property>
</bean>

      




Secondly, the User class must be annotated with an XStream alias, otherwise you will get the full package name in XML - for example: <com.rest.example.model.User>

instead of <user>

- a way to fix:

@XStreamAlias("user")
public class User {
...

      

0


source







All Articles