{8.1.0-new, 8.1.3-updated, 8.2.0-updated} Configurable Annotations

All configurable properties described in the previous section have annotation equivalents that can be applied on classes or methods.

In the section on the REST server API, we describe how to configure serializers and parsers using @Property annotations such as shown below:

// Configuring serializers/parsers using @Property annotations. @Rest( path="/atom", title="Sample ATOM feed resource", properties={ @Property(name=WSERIALIZER_quoteChar, value="'"), @Property(name=RDF_rdfxml_tab, value="5"), @Property(name=RDF_addRootProperty, value="true"), @Property(name=BEAN_examples, value="{'org.apache.juneau.dto.atom.Feed': $F{AtomFeedResource_example.json}}") } ... ) public class AtomFeedResource extends BasicRestServletJena { ... }

However, an even easier way to configure serializers and parsers are to used provided specialized @XConfig annotations like those shown below:

// Same as above but using specialized annotations. @Rest( path="/atom", title="Sample ATOM feed resource" ... ) @SerializerConfig(quoteChar="'") @RdfConfig(rdfxml_tab="5", addRootProperty="true") @BeanConfig(examples="Feed: $F{AtomFeedResource_example.json}") public class AtomFeedResource extends BasicRestServletJena { ... }

Config annotations are provided for all serializers and parsers:

- {@link oaj.annotation.BeanConfig BeanConfig}
- {@link oaj.csv.annotation.CsvConfig CsvConfig}
- {@link oaj.html.annotation.HtmlConfig HtmlConfig}
- {@link oaj.html.annotation.HtmlDocConfig HtmlDocConfig}
- {@link oaj.jso.annotation.JsoConfig JsoConfig}
- {@link oaj.json.annotation.JsonConfig JsonConfig}
- {@link oaj.jsonschema.annotation.JsonSchemaConfig JsonSchemaConfig}
- {@link oaj.msgpack.annotation.MsgPackConfig MsgPackConfig}
- {@link oaj.oapi.annotation.OpenApiConfig OpenApiConfig}
- {@link oaj.parser.annotation.ParserConfig ParserConfig}
- {@link oaj.plaintext.annotation.PlainTextConfig PlainTextConfig}
- {@link oaj.jena.annotation.RdfConfig RdfConfig}
- {@link oaj.serializer.annotation.SerializerConfig SerializerConfig}
- {@link oaj.soap.annotation.SoapXmlConfig SoapXmlConfig}
- {@link oaj.uon.annotation.UonConfig UonConfig}
- {@link oaj.urlencoding.annotation.UrlEncodingConfig UrlEncodingConfig}
- {@link oaj.xml.annotation.XmlConfig XmlConfig}

Config annotations defined on classes and methods can be applied to serializers and parsers using the following methods:

- {@link oaj.BeanContextBuilder}
  - {@link oaj.BeanContextBuilder#applyAnnotations(Class...) applyAnnotations(Class...)} - Apply annotations from class and all parent classes.
  - {@link oaj.BeanContextBuilder#applyAnnotations(Method...) applyAnnotations(Method...)} - Apply annotations from method and parent methods.

The following example shows how annotations defined on a dummy class can be applied to a serializer:

@SerializerConfig(quoteChar="'") @BeanConfig(bpi="AddressBean: street,city,state") public static class DummyClass {} WriterSerializer serializer = JsonSerializer.create().applyAnnotations(DummyClass.class).build(); String json = serializer.toString(addressBean);

Annotations can also be applied directly to serializers and parsers using the following method:

- {@link oaj.BeanContextBuilder}
  - {@link oaj.BeanContextBuilder#annotations(Annotation...) annotations(Annotation...)}

The following example shows a concrete implementation of an interface can be applied to a serializer:

public class AddressBean {...} Bean ba = new BeanAnnotation("AddressBean").bpi("street,city,state"); WriterSerializer serializer = JsonSerializer.create().annotations(ba).build(); String json = serializer.toString(addressBean); // Will print street,city,state

The following concrete annotation implementation are provided:

- {@link oaj.annotation.BeanAnnotation} implements {@link oaj.annotation.Bean}
- {@link oaj.annotation.BeancAnnotation} implements {@link oaj.annotation.Beanc}
- {@link oaj.annotation.BeanIgnoreAnnotation} implements {@link oaj.annotation.BeanIgnore}
- {@link oaj.annotation.BeanpAnnotation} implements {@link oaj.annotation.Beanp}
- {@link oaj.annotation.ExampleAnnotation} implements {@link oaj.annotation.Example}
- {@link oaj.annotation.NamePropertyAnnotation} implements {@link oaj.annotation.NameProperty}
- {@link oaj.annotation.ParentPropertyAnnotation} implements {@link oaj.annotation.ParentProperty}
- {@link oaj.annotation.SwapAnnotation} implements {@link oaj.annotation.Swap}
- {@link oaj.annotation.UriAnnotation} implements {@link oaj.annotation.URI}
- {@link oaj.csv.annotation.CsvAnnotation} implements {@link oaj.csv.annotation.Csv}
- {@link oaj.html.annotation.HtmlAnnotation} implements {@link oaj.html.annotation.Html}
- {@link oaj.jso.annotation.JsoAnnotation} implements {@link oaj.jso.annotation.Jso}
- {@link oaj.json.annotation.JsonAnnotation} implements {@link oaj.json.annotation.Json}
- {@link oaj.jsonschema.annotation.SchemaAnnotation} implements {@link oaj.jsonschema.annotation.Schema}
- {@link oaj.msgpack.annotation.MsgPackAnnotation} implements {@link oaj.msgpack.annotation.MsgPack}
- {@link oaj.oapi.annotation.OpenApiAnnotation} implements {@link oaj.oapi.annotation.OpenApi}
- {@link oaj.plaintext.annotation.PlainTextAnnotation} implements {@link oaj.plaintext.annotation.PlainText}
- {@link oaj.soap.annotation.SoapXmlAnnotation} implements {@link oaj.soap.annotation.SoapXml}
- {@link oaj.uon.annotation.UonAnnotation} implements {@link oaj.uon.annotation.Uon}
- {@link oaj.urlencoding.annotation.UrlEncodingAnnotation} implements {@link oaj.urlencoding.annotation.UrlEncoding}
- {@link oaj.xml.annotation.XmlAnnotation} implements {@link oaj.xml.annotation.Xml}

Any number of matching config or concrete annotations can be applied. They are applied in the order they are provided to the context. Therefore any values can be overridden. Config and concrete annotations also override any class or method level annotations

@Bean(bpi="street,city") // Will be overridden public class AddressBean {...} Bean ba = new BeanAnnotation("AddressBean").bpi("street,city,state"); WriterSerializer serializer = JsonSerializer.create().annotations(ba).build(); String json = serializer.toString(addressBean); // Will print street,city,state

- {@doc GlossaryAnnotations Annotations Glossary}