How to auto-grow Spring @ModelAttribute command class with a List of interface objects

Viewed 658

I'm trying to bind a command class that contains a List of objects that implement an interface in Spring (Boot 1.4.4.RELEASE).

  • Command class

    @Data
    @AllArgsConstructor
    public class CarInternetContentEditFormCommand implements EditFormCommand {
    
        @Valid
        @NotEmpty
        @JsonDeserialize(contentAs = CarInternetContentForm.class)
        private List<EditForm> editForms;
    
        public CarInternetContentEditFormCommand() {
            this.editForms = new ArrayList<>();
        }
    
        public EditForm getEditForm() {
            if (this.editForms.isEmpty()) {
                return new CarInternetContentForm();
            }
            return this.editForms.get(0);
        }
    
        @Override
        public void setEditForm(EditForm form) {
            if (this.editForms.isEmpty()) {
                this.editForms = new ArrayList<>();
            }
            this.editForms.add(form);
        }
    }
    
  • Error trace

org.springframework.beans.NullValueInNestedPathException: Invalid property 'editForms' of bean class [com.phistory.mvc.cms.command.CarInternetContentEditFormCommand]: Could not instantiate property type [com.phistory.mvc.cms.form.EditForm] to auto-grow nested property path; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.phistory.mvc.cms.form.EditForm]: Specified class is an interface

When the view renders, the editForms list in the command is empty, and in my view I try to bind element number 0, like so:

<@spring.formInput "CICEFC.editForms[0].link", "class=form-control", "text"/>

Therefore Spring tries to auto-grow the list and instantiate an EditCommand object, which is obviously an interface and it doesn't know how to.

Is there a way to tell Spring how to do this? I have tried registering a PropertyEditor for the command class but it never gets invoked.

0 Answers
Related