Play!Framework の play.data.Form 機能を使いたいのですが、以下のクラスで困っています。そのような継承構造を持つネストされたパスをフォームに認識させる方法があるかどうか、誰か教えてもらえますか? これまでのところ、私が試したことは、Json で動作させることです。これが私のコードのダイジェストです。
@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
}
詳細は次のように定義されています。
@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
}
そしてもちろん、すべてのサブクラスが (まったく異なるフィールド セットで) 実装されており、パブリック デフォルト コンストラクターが利用可能です。この構造を Json (Jackson 自体と Play フレームワークでラップされた Json 実装) でテストしたところ、動作しました。問題は、コントローラーで宣言して使用しようとしたときですForm<ContactParam>
private static final Form<ContactParam> contactForm = Form.form(ContactParam.class);
public static Result myAction() {
Form<ContactParam> form = contactForm.bindFromRequest();
if(form.hasErrors){
//...
}else{
//...
}
}
newInstance()
のメソッドを使用して抽象クラスを直接インスタンス化しようとしているため、バインドによって InstancioException が原因でエラーが発生しますClass<ContactFieldParam>
。DataBinder
Form クラス (および)で使用される springframework コードを調べることで、それを判断しました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)