此内容没有您所选择的语言版本。

Chapter 17. JAXB Providers


RESTEasy includes support for marshaling and unmarshaling JAXB annotated classes. Multiple JAXB Providers are included with RESTEasy to address the subtle differences between classes generated by XJC and classes that are annotated with @XmlRootElement, or work with JAXBElement classes directly.
When using the JAX-RS API in development, the provider to be invoked is selected transparently. This chapter describes the providers best-suited for a variety of configurations if you want to access the providers directly.
RESTEasy selects a JAXB Provider when a parameter type (return type) is an object annotated with JAXB annotations (for example, @XmlRootEntity or @XmlType), or a JAXBElement. The resource class (resource method) will be annotated with either @Consumes or @Produces, and contain one or more of the following values:
  • text/*+xml
  • application/*+xml
  • application/*+fastinfoset
  • application/*+json
RESTEasy selects different providers based on the return type used in the resource. This section describes the workings of the selection process.
Classes annotated with @XmlRootElement are handled with the JAXBXmlRootElementProvider. This provider handles basic marshaling and unmarshaling of custom JAXB entities.
Classes that are generated by XJC do not usually contain an @XmlRootElement annotation. To be marshaled, they must be wrapped in an instance of JAXBElement. This usually involves invoking a method named ObjectFactory on the class, which serves as the XmlRegistry.
The JAXBXmlTypeProvider provider is selected when the class is annotated with an XmlType annotation and not an XmlRootElement annotation. The provider attempts to locate the XmlRegistry for the target class. By default, a JAXB implementation creates a class called ObjectFactory and is located in the same package as the target class. ObjectFactory contains a create method that takes the object instance as a parameter. For example, if the target type is called Contact, then the ObjectFactory class will have a method:
      public JAXBElement createContact(Contact value) {..
Copy to Clipboard Toggle word wrap
If your resource works with the JAXBElement class directly, the RESTEasy runtime will select the JAXBElementProvider. This provider examines the ParameterizedType value of the JAXBElement in order to select the appropriate JAXBContext.

17.1. JAXB Decorators

RESTEasy's JAXB providers can decorate marshaler and Unmarshaler instances. Add an annotation that triggers the decoration marshaler or Unmarshaler. The decorators can perform tasks such as setting marshaler or Unmarshaler properties and setting up validation.
As an example, say you want to create an annotation that will trigger pretty-printing of an XML document. In raw JAXB, we would set a property on the marshaler of marshaler.JAXB_FORMATTED_OUTPUT. Instead, let us write a marshaler decorator.
First, define an annotation:
 import org.jboss.resteasy.annotations.Decorator;

 @Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
 @Retention(RetentionPolicy.RUNTIME)
 @Decorator(processor = PrettyProcessor.class, target = marshaler.class)
 public @interface Pretty {}
Copy to Clipboard Toggle word wrap
For this to work, you must annotate the @Pretty annotation with a meta-annotation named @Decorator. The target() attribute must be the JAXB marshaler class. Next, we will write the processor() attribute class.
 import org.jboss.resteasy.core.interception.DecoratorProcessor;
 import org.jboss.resteasy.annotations.DecorateTypes;

 import javax.xml.bind.marshaler;
 import javax.xml.bind.PropertyException;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.Produces;
 import java.lang.annotation.Annotation;

 /**
  * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
  * @version $Revision: 1 $
  */
 @DecorateTypes({"text/*+xml", "application/*+xml"})
 public class PrettyProcessor implements DecoratorProcessor<marshaler, Pretty>
 {
    public marshaler decorate(marshaler target, Pretty annotation,
                  Class type, Annotation[] annotations, MediaType mediaType)
    {
       target.setProperty(marshaler.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    }
 }
Copy to Clipboard Toggle word wrap
The processor implementation must implement the DecoratorProcessor interface, and should also be annotated with @DecorateTypes. This annotation specifies the media types that the processor can work with.
Now that we have defined our annotation and our Processor, we can use it on our JAX-RS resource methods or JAXB types like so:
   @GET
   @Pretty
   @Produces("application/xml")
   public SomeJAXBObject get() {...}
Copy to Clipboard Toggle word wrap
If this is confusing, check the RESTEasy source code for information about implementing @XmlHeader.
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat