Spring MVC-使用@ResponseBody时设置JAXB marshaller属性 [英] Spring MVC - set JAXB marshaller property when using @ResponseBody

查看:115
本文介绍了Spring MVC-使用@ResponseBody时设置JAXB marshaller属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从控制器返回一个对象,该对象应在spring之前解析为xml. 但是我使用了 @XmlNamedObjectGraph (来自moxy eclipselink)类中的注解以自定义返回的对象.因此,我必须从marshaller设置属性MarshallerProperties.OBJECT_GRAPH.

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.

如何在控制器中访问spring用来解析对象的编组器?

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

即:

@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);
}

谢谢您的帮助.

推荐答案

就像Sotirios Delimanolis所说的那样.您必须实现自己的AbstractJaxb2HttpMessageConverter.但除此之外,您还实现了一个WebBindingInitializer并向其注册:

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

这篇关于Spring MVC-使用@ResponseBody时设置JAXB marshaller属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆