使用 Jersey 和 Jackson 从 JSON 解组 JAXB 带注释的类

发布于 2024-12-13 13:31:35 字数 2489 浏览 0 评论 0原文

我有一个具有 JAXB 和 bean 验证注释的类,非常类似于以下内容:

@XmlType(namespace = "urn:com:company:assessment")
@XmlRootElement
public class Assessment
{
    // ...

    @NotNull
    @Size(min = 1,
          message = "Assessment requires at least one address.")
    @Valid
    @XmlJavaTypeAdapter(AddressesAdapter.class)
    public synchronized Set<Address> getAddresses()
    {
        return this.addresses;
    }

    // ...
}

Jersey 将此类编组到 XML 并从 XML 解组就好了;但是,Jersey 无法正确从 JSON 中解组。我在 AddressesAdapter 类中设置了断点,并注意到在反序列化 JSON 时永远不会调用 unmarshal() 方法。

无论是否使用自然表示法或其他方式配置 Jersey,如果我创建 @XmlAnyElement 带注释的属性,我都会获得应该使用适配器解组的 addresses 元素。

如果我直接使用 Jackson 并注册其 JacksonJaxbJsonProvider 提供程序,则在尝试从 JSON 解组时会出现异常:(

Nov 2, 2011 10:07:41 AM com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: A message body reader for Java class com.company.Assessment, and Java type class com.company.Assessment, and MIME media type application/json was not found
Nov 2, 2011 10:07:41 AM com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: The registered message body readers compatible with the MIME media type are:
*/* ->
  com.sun.jersey.core.impl.provider.entity.FormProvider
  com.sun.jersey.core.impl.provider.entity.StringProvider
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.ReaderProvider
  com.sun.jersey.core.impl.provider.entity.DocumentProvider
  com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
  com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
  com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
  com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
  com.sun.jersey.core.impl.provider.entity.EntityHolderReader

JacksonJaxbJsonProvider 未列出。)

有谁知道如何处理我可以将 JAXB 注释的类编组为 XML 和 JSON,而无需创建 MessageBodyReaderMessageBodyWriter 吗?

提前致谢!

I have a class that has JAXB and bean validation annotations, much like the following:

@XmlType(namespace = "urn:com:company:assessment")
@XmlRootElement
public class Assessment
{
    // ...

    @NotNull
    @Size(min = 1,
          message = "Assessment requires at least one address.")
    @Valid
    @XmlJavaTypeAdapter(AddressesAdapter.class)
    public synchronized Set<Address> getAddresses()
    {
        return this.addresses;
    }

    // ...
}

Jersey marshals this class to and unmarshals from XML just fine; however, Jersey can't unmarshal from JSON correctly. I've set breakpoints within the AddressesAdapter class, and noticed that the unmarshal() method is never invoked when unserializing JSON.

Despite whether configuring Jersey using natural notation or otherwise, if I create an @XmlAnyElement-annotated property, I get addresses elements that should have been unmarshalled using the adapter.

If I use Jackson directly and register its JacksonJaxbJsonProvider provider, I get exceptions when attempting to unmarshal from JSON:

Nov 2, 2011 10:07:41 AM com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: A message body reader for Java class com.company.Assessment, and Java type class com.company.Assessment, and MIME media type application/json was not found
Nov 2, 2011 10:07:41 AM com.sun.jersey.api.client.ClientResponse getEntity
SEVERE: The registered message body readers compatible with the MIME media type are:
*/* ->
  com.sun.jersey.core.impl.provider.entity.FormProvider
  com.sun.jersey.core.impl.provider.entity.StringProvider
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.ReaderProvider
  com.sun.jersey.core.impl.provider.entity.DocumentProvider
  com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
  com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
  com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
  com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
  com.sun.jersey.core.impl.provider.entity.EntityHolderReader

(JacksonJaxbJsonProvider is not listed.)

Does anyone have an idea as to how I may marshal my JAXB-annotated classes as both XML and JSON without having to create MessageBodyReaders and MessageBodyWriters?

Thanks in advance!

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

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

发布评论

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

评论(2

相权↑美人 2024-12-20 13:31:35

将 jersey-json 添加到项目依赖项中。

Add jersey-json to your project dependencies.

毁梦 2024-12-20 13:31:35

我想你可能也遇到了和我昨天一样的问题。我对 Jersey 文档感到困惑,不理解所需的所有要求。

我创建了一个小型 Jersey 客户端,它从 Rails 服务器获取 JSON 资源并将其解组到 POJO 中,但昨天我无法让它工作,但今天我成功了!

首先,Jersey 需求文档中缺少一个 jar,即 Jackson XML Compability jar,而且文档也很混乱。我写了一个小错误报告 http://java.net/jira/browse/JERSEY-803

确保运行程序时有以下 jar:

  • jackson-core-asl.jar
  • jackson-mapper-asl.jar
  • jackson-jaxrs.jar
  • jackson-xc.jar
  • jettison.jar
  • activation.jar
  • stax-api.jar
  • jersey-json.jar

您还需要 JAXB 2.x 支持,但这是 Java SE 6 的一部分。

请告诉我这是否适合您,如果不适合,我可以发布我的 JSON 客户端代码。

I think you might have stumbled upon the same problem as I did yesterday. I was confused by the Jersey documentation and didn't understand all the requirements that are needed.

I have created a small Jersey Client that gets a JSON resource from a Rails server and unmarshalles it into a POJO but I could not get it to work yesterday but today I was successful!

First there is one jar missing in the Jersey requirements documentation, the Jackson XML Compability jar and also the documentation is confusing. I wrote a small bugg report on that http://java.net/jira/browse/JERSEY-803

Make sure you have the following jars when running your program:

  • jackson-core-asl.jar
  • jackson-mapper-asl.jar
  • jackson-jaxrs.jar
  • jackson-xc.jar
  • jettison.jar
  • activation.jar
  • stax-api.jar
  • jersey-json.jar

You will also require JAXB 2.x support but thats part of Java SE 6.

Let me know if this works for you, if not I can post my JSON client code.

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