Spring MVC - set JAXB marshaller property when using @ResponseBody

本小妞迷上赌 提交于 2019-12-24 09:28:29

问题


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.


回答1:


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" />



回答2:


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>



回答3:


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"/>


来源:https://stackoverflow.com/questions/21798952/spring-mvc-set-jaxb-marshaller-property-when-using-responsebody

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!