Jersey + Moxy + JAXB - how to marshal XML without annotations

本小妞迷上赌 提交于 2019-12-04 16:51:09
bdoughan

To have Jersey pick up MOXy as the JAXB provider, you simply need to add the jaxb.properties file with the correct entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html) in the same package as your domain model. In the case of JAX-RS if a ContextResolver is not specified this package needs to be the one that corresponds to the parameter to, or return type from from the method in your service mapped with JAX-RS annotations.

Note:

There was a bug in an earlier version of Jersey that prevented MOXy from being picked up as the default JAXB provider, in that case you could:

  1. Upgrade to a newer version of Jersey.
  2. Create a ContextResolver to return an instance of a MOXy JAXBContext (see: http://blog.bdoughan.com/2011/04/moxys-xml-metadata-in-jax-rs-service.html)

In the case of the ContextResolver you could use code to directly create the MOXy JAXBContext instead of leveraging a jaxb.properites file. Refer to option #2 in the answer I linked to below:

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).

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.

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!