Play.data.Form - understanding subclasses in the Java play framework

I would like to use the play.data.Form function for Play! Framework, but I have a problem with the following classes. Can someone tell me if there is a way to inform forms about nested paths with such inheritance structures? So far, I have been trying to get it to work with Json. Here is the digest of my code.

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class ContactParam implements IEntityParam<Contact> {

    private Long id;

    private String name;

    private List<ContactFieldParam> contacts;

    private Long companyId;

    //standard getters and setters
}

with information defined as follows:

@JsonSubTypes({
        @Type(EmailFieldParam.class),
        @Type(PhoneFieldParam.class),
        @Type(SocialNetworkFieldParam.class),
        @Type(AddressFieldParam.class),
        @Type(WebsiteUrlFieldParam.class)
})
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "ctype")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public abstract class ContactFieldParam implements IEntityParam<ContactField> {

    private Long id;

    //standard getters and setters
}

and, of course, all subclasses are implemented (with a completely different set of fields) and have an open default constructor. I tested this structure with Json (Jackson by itself and a wrapped implementation of Json on the game system) and it works. The problem is that I'm trying to use it in my controller by declaringForm<ContactParam>

private static final Form<ContactParam> contactForm = Form.form(ContactParam.class);

public static Result myAction() {
     Form<ContactParam> form = contactForm.bindFromRequest();
     if(form.hasErrors){
     //...
     }else{
     //...
     }
}

, InstanciationException, , newInstance() Class<ContactFieldParam>. , , - , Form (DataBinder BeanWrapperImpl).

org.springframework.beans.InvalidPropertyException: Invalid property 'contacts[0]' of bean class [crud.params.contact.ContactParam]: Illegal attempt to get property 'contacts' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'contacts' of bean class [crud.params.contact.ContactParam]: Could not instantiate property type [crud.params.contact.fields.ContactFieldParam] to auto-grow nested property path: java.lang.InstantiationException
[...]
Caused by: org.springframework.beans.NullValueInNestedPathException: Invalid property 'contacts' of bean class [crud.params.contact.ContactParam]: Could not instantiate property type [crud.params.contact.fields.ContactFieldParam] to auto-grow nested property path: java.lang.InstantiationException
    at org.springframework.beans.BeanWrapperImpl.newValue(BeanWrapperImpl.java:633)
+4

Source: https://habr.com/ru/post/1525556/


All Articles