{title:'Context Annotations', created:'8.1.0', updated:'8.1.3,8.2.0,9.0.0'}

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 @XConfig annotations like those shown below:

| @Rest( | path="/addressBook", | title="Address Book REST API" | ... | ) | @SerializerConfig(quoteChar="'") | @RdfConfig(rdfxml_tab="5", addRootProperty="true") | @BeanConfig(sortProperties="true", examples="Feed: $F{AddressBook_example.json}") | @Bean(onClass=Address.class, properties="street,city,state") | public class AddressBookResource extends BasicRestServlet { | ... | }

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

- {@link oaj.BeanContext.Builder}
  - {@link oaj.BeanContext.Builder#applyAnnotations(Class...) applyAnnotations(Class...)}
  - {@link oaj.BeanContext.Builder#applyAnnotations(Method...) applyAnnotations(Method...)}

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

| @SerializerConfig(quoteChar="'") | @Bean(on="Address", properties="street,city,state") | public static class DummyClass {} | | WriterSerializer serializer = JsonSerializer.create().applyAnnotations(DummyClass.class).build(); | String json = serializer.toString(addressBean);

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

Annotations normally applied to bean classes/methods/fields/parameters can also be programmatically attatched to beans by using the "on" or "onClass" annotation values as seen on the @Bean annotation in the example above. These include:

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

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

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

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

Concrete annotation implementations are provided for all annotations.

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(properties="street,city") // Will be overridden | public class Address {...} | | Bean beanAnnotation = new BeanAnnotation("Address").properties("street,city,state"); | WriterSerializer serializer = JsonSerializer.create().annotations(beanAnnotation).build(); | String json = serializer.toString(addressBean); // Will print street,city,state