轻松休息 +春天 +杰克逊 +贾克斯布

发布于 2024-10-17 22:19:18 字数 412 浏览 5 评论 0原文

我正在尝试使用 RESTEasy 与 Jackson 序列化 JAXB 带注释的类。默认情况下,ResteasyJacksonProvider 配置为仅使用 JACKSON 注释。有没有办法使用 spring 配置 ResteasyJacksonProvider 以使用 JAXB 注释?有几种编程方式,但如果有一些弹簧配置,我会更喜欢。

我正在考虑的几种方法

  1. 使用 ContextResolver for ObjectMapper 类型返回配置为使用 JaxbAnnotationIntrospector 而不是 JacksonAnnotationIntrospector 的

  2. 扩展 ResteasyJacksonProvider 并在构建过程中传递 JAXB 注解。

还有其他办法吗?

I am trying to serialize JAXB annotated class with Jackson using RESTEasy. By default ResteasyJacksonProvider is configured to use JACKSON annotation only. Is there a way to configure ResteasyJacksonProvider to use JAXB annotation using spring? There are couple of programmatic ways but would prefer if there is some spring configuration.

Couple of ways I am thinking of

  1. Use ContextResolver for ObjectMapper type to return ObjectMapper configured to use JaxbAnnotationIntrospector instead of JacksonAnnotationIntrospector.

  2. Extend ResteasyJacksonProvider and pass JAXB annotation during construction.

Any other way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人练习一个人 2024-10-24 22:19:18

好吧,使用 ContextResolver 的第一个选项是有效的,但我仍然认为应该有一种更简单的方法来通过一些配置来做到这一点。

Well the first option of using ContextResolver works but I still think there should be an easier way to do this just by some configuration.

夕嗳→ 2024-10-24 22:19:18

您只需从配置中获取此信息,无需进行任何特殊编程。
以下是如何:
首先正确设置您的配置,我使用 Jackson + JAXB,两者都在 ContentNegotiatingViewResolver bean 下设置:

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1"/>
        <property name="mediaTypes">
            <map>
                <entry key="xml" value="application/xml" />
                <entry key="json" value="application/json" />
            </map>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                    <property name="marshaller">
                        <oxm:jaxb2-marshaller id="marshaller">
                            <oxm:class-to-be-bound name="com.shay.dashboard.data.structure.page.PageObject" />
                            <oxm:class-to-be-bound name="com.shay.dashboard.data.structure.tab.TabObject" />
                            <oxm:class-to-be-bound name="com.shay.dashboard.data.structure.section.SectionObject" />
                            <oxm:class-to-be-bound name="com.shay.dashboard.data.structure.element.nonembedded.ElementObject"/>
                            <oxm:class-to-be-bound name="com.shay.dashboard.data.structure.element.embedded.EmbeddedElementObject"/>
                            <oxm:class-to-be-bound name="com.shay.dashboard.data.structure.chart.common.ChartManager"/>
                        </oxm:jaxb2-marshaller>
                    </property>
                </bean>
                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
            </list>
        </property>
    </bean>

请注意,在编组器下我设置了 oxm:class-to-be-bound - 这些是要由 JAXB 绑定的类。

现在对于模块,我使用普通注释包(javax.xml.bind.annotation),非编组器特定的。 Jackson Json 和 JAXB 都知道如何阅读它。

例如:

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(名称=“页面”)
公共类 PageObject 实现 ComponentTypeObject{
@XmlAttribute(名称=“名称”)
私有字符串名称;
@XmlAttribute(名称=“id”,必需= true)
私有字符串 ID;
@XmlElements({@XmlElement(name="tab", type=TabXmlAdapter.class)})
私有列表标签引用;

最后,MVC 的控制器需要返回模型和视图:

    @RequestMapping(value="/get_page", method = RequestMethod.GET)
public ModelAndView initPage()
{
    ModelAndView mav = null;
    try
    {
        PageObject myPage = (PageObject) Utilities.getUtilities().loadObjectFromFile(XmlComponentType.page);
        mav = new ModelAndView("page","page",myPage);
    }
    catch (Exception e)
    { 
        e.getMessage();
    }
    return mav;
}

现在,在调用以 .json 结尾的 URL 时,您将获得 JSON 表示形式,以及 .xml 和 XML 表示形式。如果您在注释模块时给出了正确的映射,则两者都会由查看器翻译。

You can get this from the configuration only, no need to program anything special.
Here's how to:
First set your configuration right, I use Jackson + JAXB, both set under ContentNegotiatingViewResolver bean:

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1"/>
        <property name="mediaTypes">
            <map>
                <entry key="xml" value="application/xml" />
                <entry key="json" value="application/json" />
            </map>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                    <property name="marshaller">
                        <oxm:jaxb2-marshaller id="marshaller">
                            <oxm:class-to-be-bound name="com.shay.dashboard.data.structure.page.PageObject" />
                            <oxm:class-to-be-bound name="com.shay.dashboard.data.structure.tab.TabObject" />
                            <oxm:class-to-be-bound name="com.shay.dashboard.data.structure.section.SectionObject" />
                            <oxm:class-to-be-bound name="com.shay.dashboard.data.structure.element.nonembedded.ElementObject"/>
                            <oxm:class-to-be-bound name="com.shay.dashboard.data.structure.element.embedded.EmbeddedElementObject"/>
                            <oxm:class-to-be-bound name="com.shay.dashboard.data.structure.chart.common.ChartManager"/>
                        </oxm:jaxb2-marshaller>
                    </property>
                </bean>
                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
            </list>
        </property>
    </bean>

Notice that under the marshaller I set the oxm:class-to-be-bound - those are the classes to be bound by JAXB.

Now for the module, I used ordinary annotation package (javax.xml.bind.annotation), non marshaller specific. Jackson Json and JAXB both know how to read it.

For example:

@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name="page")
public class PageObject implements ComponentTypeObject{
@XmlAttribute(name="name")
private String name;
@XmlAttribute(name="id",required=true)
private String id;
@XmlElements({@XmlElement(name="tab", type=TabXmlAdapter.class)})
private List<TabXmlAdapter> tabRef;

Finally the controller for your MVC needs to return a model and view:

    @RequestMapping(value="/get_page", method = RequestMethod.GET)
public ModelAndView initPage()
{
    ModelAndView mav = null;
    try
    {
        PageObject myPage = (PageObject) Utilities.getUtilities().loadObjectFromFile(XmlComponentType.page);
        mav = new ModelAndView("page","page",myPage);
    }
    catch (Exception e)
    { 
        e.getMessage();
    }
    return mav;
}

Now while calling your URL ending with .json you'd get the JSON representation, and with .xml - and XML. Both are translated by the viewer, provided you gave the correct mapping when annotating the module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文