Domanda

I am trying to return a object from my controller which should be parsed to xml by spring. But I used the @XmlNamedObjectGraph (from moxy eclipselink) annotation in my class to customize the returned object. So I have to set the property MarshallerProperties.OBJECT_GRAPH from the marshaller.

How can I access the marshaller, which is used by spring to parse my object, in my controller?

ie:

@RequestMapping(value = "/xml/", method = RequestMethod.GET, produces = "application/xml")
@ResponseBody
public ResponseEntity<Customer> getXml() {
    Customer customer = _customerService.getById(12);
    ...
    marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, "default");
    ...
    return new ResponseEntity<>(customer, HttpStatus.OK);
}

Thanks for your help in advance.

È stato utile?

Soluzione

It is like Sotirios Delimanolis said. You have to implement your own AbstractJaxb2HttpMessageConverter. But additional to that you have implementet an WebBindingInitializer and register it with:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="com.example.CommonWebBindingInitializer" />
    </property>
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
            <bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />
            <bean class="com.example.Jaxb2RootElementHttpMessageConverter" />
        </list>
    </property>
</bean>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

Altri suggerimenti

You'll need to implement your own AbstractJaxb2HttpMessageConverter class and override its createMarshaller method to provide a Marshaller with your own properties. Look at Jaxb2RootElementHttpMessageConverter for implementation hints.

Once you've implemented such a class, you'll need to register it as a HttpMessageConverter with your MVC stack. If you're doing your configuration through Java, look into WebMvcConfigurationSupport#configureMessageConverters(..). If you are doing it through XML, look into

<mvc:annotation-driven>
    <mvc:message-converters>
        <!-- bean goes here -->
    </mvc:message-converters>
</mvc:annotation-driven>

If you need to customize the marshaller, create a marshalling view and configure the marshaller with the properties you need, this is an example of configuring a JAXB marshaller (see this answer):

<!-- XML view using a JAXB marshaller -->
<bean id="jaxbView" class="org.springframework.web.servlet.view.xml.MarshallingView">
    <constructor-arg>
        <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
            <property name="classesToBeBound">
                <list>
                    <value>com.company.AClass</value>
                </list>
            </property>
        </bean>
    </constructor-arg>
</bean>

<!-- Resolve views based on string names -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top