Java Beans Support
Out-of-the-box, Juneau supports marshalling of Java beans with standard public getters and setters, public fields, and
fluent setters (e.g.
withX
naming convention).
There are also many settings and annotations that can be used to customize how bean properties are detected.
The following is an example of some of the ways to define bean properties:
public class MyBean {
// Public field property.
public String property1;
// Standard public getters/setters.
public String getProperty2() {...}
public void setProperty2(String value) {...}
// With fluent-style setter.
public String getProperty3() {...}
public MyBean withProperty3(String value) {...}
// Read-only property (ignored by parsers).
public String getProperty4() {...}
// Write-only property (ignored by serializers).
public void setProperty5(String value) {...}
// Non-standard getters/setters identified by annotation.
@Beanp
public String property6() {...}
@Beanp
public void property6(String value) {...}
// Non-standard getters/setters identified by annotation with overridden names.
@Beanp("property7")
public String property7X() {...}
@Beanp("property7")
public void property7X(String value) {...}
// Non-public getters/setters identified by annotation.
@Beanp
private String getProperty8() {...}
@Beanp
private void setProperty8(String value) {...}
// Ignore a method that looks like a getter.
@BeanIgnore
public String getNotAProperty() {...}
}
Several settings exist to allow you to customize how bean properties are handled by serializers and parsers:
Settings and equivalent annotations are also available to control which properties are marshalled and how they are ordered.
It's common to use the @Bean(properties|p) annotation to force the ordering of properties during marshalling. IBM JVMs keep the ordering of fields and methods in the compiled bytecodebut Oracle JVMs do not and return fields/methods in random order. The @Bean(properties|p) annotation was added to help with this limitation.
// Bean should be marshalled with properties in the specified order.
@Bean(properties="foo,bar,baz")
public class MyBean {
...
}
- POJO Categories - Marshalling rules for POJOs.
- Bean Dictionaries - Handling properties with subclassable types.