이 콘텐츠는 선택한 언어로 제공되지 않습니다.

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) {..
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 {}
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);
    }
 }
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() {...}
If this is confusing, check the RESTEasy source code for information about implementing @XmlHeader.
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2026 Red Hat
맨 위로 이동