使用 Jersey 和 Jackson 从 JSON 解组 JAXB 带注释的类
我有一个具有 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,而无需创建 MessageBodyReader
和 MessageBodyWriter
吗?
提前致谢!
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 MessageBodyReader
s and MessageBodyWriter
s?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 jersey-json 添加到项目依赖项中。
Add jersey-json to your project dependencies.
我想你可能也遇到了和我昨天一样的问题。我对 Jersey 文档感到困惑,不理解所需的所有要求。
我创建了一个小型 Jersey 客户端,它从 Rails 服务器获取 JSON 资源并将其解组到 POJO 中,但昨天我无法让它工作,但今天我成功了!
首先,Jersey 需求文档中缺少一个 jar,即 Jackson XML Compability jar,而且文档也很混乱。我写了一个小错误报告 http://java.net/jira/browse/JERSEY-803
确保运行程序时有以下 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:
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.