Skip to main content

Virtual Beans

By default, interface proxies are enabled, which allows the Juneau parsers to parse content into virtual beans (bean interfaces without implementation classes). Calling MarshallingContext.Builder.disableInterfaceProxies() turns this behavior off.

For example, the following code creates an instance of the specified unimplemented interface:

// Our unimplemented interface
public interface Address {

String getStreet();
void setStreet(String value);

String getCity();
void setCity(String value);

StateEnum getState();
void setState(StateEnum value);

int getZip();
void setZip(int value);
}

// Our code
Address address = Json.to(
"{street:'123 Main St', city:'Anywhere', state:'PR', zip:12345}",
Address.class
);

int zip = address.getZip();
address.setState(StateEnum.NY);

Getter and setter values can be any parsable values, even other virtual beans.

Under-the-covers, a virtual bean is simply a proxy interface on top of an existing BeanMap instance. From a programmatic point-of-view, they're indistinguishable from real beans, and can be manipulated and serialized like any other bean.

Virtual beans can also be created programmatically using the MarshallingContext class:

Address address = MarshallingContext.DEFAULT.createSession().build().newBean(Address.class);