{8.2.0-new} 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:

- {@link oaj.BeanContext}
  - {@link oaj.BeanContext#BEAN_beanClassVisibility BEAN_beanClassVisibility} - Allows non-public POJOs to be treated as beans.
  - {@link oaj.BeanContext#BEAN_beanConstructorVisibility BEAN_beanConstructorVisibility} - Allows non-public constructors to be used on beans.
  - {@link oaj.BeanContext#BEAN_beanFieldVisibility BEAN_beanFieldVisibility} - Allows non-public fields to be used as bean properties.
  - {@link oaj.BeanContext#BEAN_beanMethodVisibility BEAN_beanMethodVisibility} - Allows non-public methods to be used as bean getters and setters.
  - {@link oaj.BeanContext#BEAN_beansRequireDefaultConstructor BEAN_beansRequireDefaultConstructor} - Don't treat a POJO as a bean if it doesn't have a default no-arg constructor.
  - {@link oaj.BeanContext#BEAN_beansRequireSerializable BEAN_beansRequireSerializable} - Don't treat a POJO as a bean if it doesn't implement Serializable.
  - {@link oaj.BeanContext#BEAN_beansRequireSettersForGetters BEAN_beansRequireSettersForGetters} - Ignore getter methods if they don't have setter methods.
  - {@link oaj.BeanContext#BEAN_beansRequireSomeProperties BEAN_beansRequireSomeProperties} - Don't treat a POJO as a bean if it doesn't have at least one property defined.

Settings and equivalent annotations are also available to control which properties are marshalled and how they are ordered.

- {@link oaj.BeanContext}
  - {@link oaj.BeanContext#BEAN_bpi BEAN_bpi} - Specifies properties to include and property order during marshalling.
  - {@link oaj.BeanContext#BEAN_bpx BEAN_bpx} - Specifies properties to exclude during marshalling.
  - {@link oaj.BeanContext#BEAN_bpro BEAN_bpro} - Specifies read-only properties (ignored during parsing).
  - {@link oaj.BeanContext#BEAN_bpwo BEAN_bpwo} - Specifies write-only properties (ignored during reading).

It's common to use the {@link oaj.annotation.Bean#bpi @Bean(bpi)} annotation to force the ordering of properties during marshalling. IBM JVMs keep the ordering of fields and methods in the compiled bytecode, but Oracle JVMs do not and return fields/methods in random order. The {@link oaj.annotation.Bean#bpi @Bean(bpi)} annotation was added to help with this limitation.

// Bean should be marshalled with properties in the specified order. @Bean(bpi="foo,bar,baz") public class MyBean { ... }

Another option to force ordering of properties is to use {@link oaj.BeanContext#BEAN_sortProperties} or {@link oaj.annotation.Bean#sort @Beanå(sort)}.

@Bean(sort=true) public class MyBean { ... }

- {@doc PojoCategories POJO Categories} - Marshalling rules for POJOs.
- {@doc BeanDictionaries Bean Dictionaries} - Handling properties with subclassable types.
- {@doc BeanAnnotation @Bean Annotation} - Details on the @Bean annotation on bean classes.
- {@doc BeanpAnnotation @Beanp Annotation} - Details on the @Beanp annotation on bean methods.
- {@doc BeancAnnotation @Beanc Annotation} - Details on the @Beanc annotation on bean constructors.
- {@doc BeanIgnoreAnnotation @BeanIgnore Annotation} - Details on the @BeanIgnore annotation classes and methods.
- {@doc PojoBuilders POJO Builders} - Parsing into unmodifiable beans using builders.
- {@doc BeanFilters Bean filters} - Programmatic filters for controlling bean properties.
- {@doc InterfaceFilters Interface filters} - Limiting bean properties to those exposed through interfaces.