Skip to main content

@YamlConfig Annotation

The @YamlConfig annotation is used to apply YAML serializer and parser configuration annotations to REST resources, methods, and other contexts.

When to use it​

Use @YamlConfig to set YAML options once on a class, method or REST resource, instead of calling builder methods on every serializer. In a plain program you apply it by passing the annotated class to a builder's applyAnnotations; in a REST resource Juneau applies it for you.

Example​

import org.apache.juneau.marshall.*;
import org.apache.juneau.marshall.yaml.*;

public class YamlAnnotationExample {

@Marshalled(typeName="person")
public static class Person {
public String name = "John Smith";
}

public static class Holder {
public Object item = new Person();
}

// The annotation turns on _type properties for any serializer that applies this class.
@YamlConfig(addBeanTypes="true")
public static class Config {}

public static void main(String[] args) throws Exception {
YamlSerializer plain = YamlSerializer.create()
.beanDictionary(Person.class)
.build();
YamlSerializer typed = YamlSerializer.create()
.beanDictionary(Person.class)
.applyAnnotations(Config.class)
.build();

System.out.println(plain.write(new Holder()));
System.out.println(typed.write(new Holder()));
}
}

The first serializer writes name: John Smith under item; the second adds _type: person there, because the declared type Object hides the runtime type.

Options​

ElementEffect
addBeanTypesAdd _type properties to beans whose runtime type differs from the declared type. A true or false string, resolved through SVL.
rankOverrides the default ordering when several config annotations apply to the same target.

REST Integration​

YAML support can be added to REST resources using the BasicYamlConfig interface or by including the YAML serializer and parser in BasicUniversalConfig:

// YAML-only REST resource
@Rest
class MyYamlResource extends RestServlet implements BasicYamlConfig {

@RestGet("/person/{id}")
public Person getPerson(@Path int id) {
return findPerson(id);
}
}

// Universal resource supporting JSON, XML, HTML, UON, and YAML
@Rest
class MyUniversalResource extends RestServlet implements BasicUniversalConfig {

@RestGet("/person/{id}")
public Person getPerson(@Path int id) {
return findPerson(id);
}
}

Custom Configuration​

You can configure the YAML serializer and parser through annotation or programmatic builders:

@Rest(
serializers = YamlSerializer.class,
parsers = YamlParser.class,
defaultAccept = "application/yaml"
)
class MyResource extends RestServlet {
// ...
}

See also​