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.Builder}
- {@link oaj.BeanContext.Builder#beanClassVisibility(Visibility) beanClassVisibility(Visibility)}
- {@link oaj.BeanContext.Builder#beanConstructorVisibility(Visibility) beanConstructorVisibility(Visibility)}
- {@link oaj.BeanContext.Builder#beanFieldVisibility(Visibility) beanFieldVisibility(Visibility)}
- {@link oaj.BeanContext.Builder#beanMethodVisibility(Visibility) beanMethodVisibility(Visibility)}
- {@link oaj.BeanContext.Builder#beansRequireDefaultConstructor() beansRequireDefaultConstructor()}
- {@link oaj.BeanContext.Builder#beansRequireSerializable() beansRequireSerializable()}
- {@link oaj.BeanContext.Builder#beansRequireSettersForGetters() beansRequireSettersForGetters()}
- {@link oaj.BeanContext.Builder#disableBeansRequireSomeProperties() disableBeansRequireSomeProperties()}
Settings and equivalent annotations are also available to control which properties are marshalled and how they are ordered.
- {@link oaj.BeanContext.Builder}
- {@link oaj.BeanContext.Builder#beanProperties(Class,String) beanProperties(Class,String)}
- {@link oaj.BeanContext.Builder#beanPropertiesExcludes(Class,String) beanPropertiesExcludes(Class,String)}
- {@link oaj.BeanContext.Builder#beanPropertiesReadOnly(Class,String) beanPropertiesReadOnly(Class,String) }
- {@link oaj.BeanContext.Builder#beanPropertiesWriteOnly(Class,String) beanPropertiesWriteOnly(Class,String)}
It's common to use the {@link oaj.annotation.Bean#properties @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 {@link oaj.annotation.Bean#properties @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 {
| ...
| }