It seems you need a separate class for UserAddress to wrap houseNo , city , state and locality properties.
+ UserInfo (firstName, lastName, technology) | + UserAddress (houseNo, locality, city, state)
Just reflect the structure specified in your Sling models.
Create a UserAddress Model:
@Model(adaptables = Resource.class) public class UserAddress { @Inject private String houseNo; @Inject private String locality; @Inject private String city; @Inject private String state;
This model can then be used in your UserInfo class:
@Model(adaptables = Resource.class) public class UserInfo { @Inject @Optional private UserAddress userAddress; public UserAddress getUserAddress() { return this.userAddress; }
You can customize the behavior with additional annotations for the default values ββand optional fields, but this is a general idea.
In general, Sling models should be able to handle the injection of another model if it finds a suitable adaptable one. This is another Sling model in this case, but I did it with inherited classes based on adapter factories.
source share