Jersey + Moxy + JAXB - 如何在没有注释的情况下编组XML [英] Jersey + Moxy + JAXB - how to marshal XML without annotations

查看:81
本文介绍了Jersey + Moxy + JAXB - 如何在没有注释的情况下编组XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://blog.bdoughan .com / 2013/06 / moxy-is-new-default-json-binding.html 大约有一半的标题是自定义JSON绑定。您如何类似地自定义XML绑定?

In http://blog.bdoughan.com/2013/06/moxy-is-new-default-json-binding.html about halfway down there's a heading "Customizing the JSON-Binding". How do you similarly customize the XML binding?

Jersey处理Moxy JSON绑定和XML等效绑定的方式似乎有根本区别。如果我按照Jersey文档中的说明创建自定义JAXBContext解析器来配置Moxy的映射文件,则该解析器将在JSON情况下触发,但不会在XML情况下触发。请参阅 https://bitbucket.org/jmetcher/resttest/ ,了解一个非常小的项目。

There seem to be fundamental differences between the way Jersey handles Moxy JSON binding and the XML equivalent. If I follow the instructions in the Jersey documentation for creating a custom JAXBContext resolver to configure Moxy's mapping file, that resolver fires in the JSON case but not in the XML case. See https://bitbucket.org/jmetcher/resttest/ for a very small project demonstrating this.

我有正确的jaxb.properties文件,我正在使用jersey-media-moxy模块构建。我可以在独立模式下使用相同的Moxy JAXB设置,我只是不能让Jersey注意它。

I have the correct jaxb.properties file in place, and I'm building with the jersey-media-moxy module. I can get the identical Moxy JAXB setup working in standalone mode, I just can't get Jersey to take any notice of it.

我已经完成的调试表明使其工作的唯一方法是创建自定义MessageBodyWriter。 jersey-media-moxy模块注册一个JSON MessageBodyWriter,然后调用任何已注册的ContextResolvers。它没有为XML注册等效的MessageBodyWriter,而Jersey的默认MBW似乎完全忽略了已注册的ContextResolvers。

The debugging I've done indicates that the only way to get this to work is to create a custom MessageBodyWriter. The jersey-media-moxy module registers a JSON MessageBodyWriter which will then invoke any registered ContextResolvers. It does not register an equivalent MessageBodyWriter for XML, and Jersey's default MBW's seem to completely ignore registered ContextResolvers.

但是,需要创建自定义MessageBodyWriter的事实是完全的从文档中遗漏,几乎每个例子似乎都表明我缺少一些基本的东西。

Howver, the fact that the need to create a custom MessageBodyWriter is completely missing from the docs and nearly every example seems to indicate that I'm missing something fundamental.

我提供了更多信息的链接,但我似乎没有代表在SO上做了很多。

I'd provide links to more info but I don't seem to have the rep to do much at all on SO.

编辑:回复评论的更多信息:

More info in response to comments:

Moxy肯定会被选为JAXB提供商。我可以在调试器中看到这一点。

Moxy is certainly being picked up as the JAXB provider. I can see this in the debugger.

Jersey版本是2.9,EclipseLink版本是2.5。 Java 7。

Jersey version is 2.9, EclipseLink version is 2.5. Java 7.

ContextResolver方法适用于JSON。在Application类中注册MoxyXMLFeature的方法也是如此。在这两种情况下,都使用使用正确映射文件配置的正确上下文。在XML的情况下,似乎Jersey甚至都没有尝试创建上下文。

The ContextResolver approach works fine for JSON. So does the approach of registering MoxyXMLFeature in an Application class. In both cases, the correct context configured with the right mapping file is used. In the XML case, it seems that Jersey never even attempts to create a context.

但是,如果我将@XmlRootElement注释添加到我正在尝试的类中元帅,一切正常。即使没有其他注释,也可以根据oxm映射文件将类封送到XML和JSON。我已经更新了bitbucket项目来展示这个。

IF, however, I add an @XmlRootElement annotation to the class I'm trying to marshal, it all works. Even with no other annotations, the class can be marshaled to both XML and JSON according to the oxm mapping file. I've updated the bitbucket project to show this.

因此,我可以通过创建自定义MBW或提供带注释的根类来解决此问题。我想在这一点上我想弄清楚最好的或推荐的方法是什么,所以我可以记录一个文档补丁。

So, I can work around the issue either by creating a custom MBW or providing an annotated root class. I guess at this point I'm to work out what the best or recommended approach would be so I can log a documentation patch.

推荐答案

<事实上,使用Jersey + Moxy将未注释的域模型编组为XML的唯一方法是注册您自己的提供者(也就是MessageBodyWriter / MessageBodyReader)。

It is indeed the case that the only way to marshal an unannotated domain model to XML using Jersey + Moxy is to register your own provider (aka MessageBodyWriter/MessageBodyReader).

支持Moha功能支持编组未注释的模型。但是,Jersey的默认JAXB提供程序通过要求在将控制权传递给Moxy之前对模型进行注释来掩盖此功能。 JSON案例的实现方式不同,没有此限制。

Marshalling an unannotated model is supported Moxy functionality. However, Jersey's default JAXB providers mask this functionality by requiring that the model is annotated before they will pass off control to Moxy. The JSON case is implemented differently and does not have this restriction.

参见 http://lagod.id.au/blog/?p=472 了解一个完整的例子。

See http://lagod.id.au/blog/?p=472 for a fully worked example.

Doc bug报告: https://java.net/jira/browse/JERSEY-2552

Doc bug reported: https://java.net/jira/browse/JERSEY-2552

这篇关于Jersey + Moxy + JAXB - 如何在没有注释的情况下编组XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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