javaspringspring-mvcspring-mvc-initbinders

Failed to convert value of type 'org.springframework.web.multipart.commons.CommonsMultipartFile' to required type


I have following controller method:

@RequestMapping(value = "/owner/terminals/save", method = RequestMethod.POST)
public String saveTerminal( @RequestParam(value = "name") String name, 
                            @RequestParam(value = "file") @Valid OnlyForImagesFileWrapper file,
                               BindingResult bindingResult )
             {
                ...

and I see the following stacktrace:

    org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'org.springframework.web.multipart.commons.CommonsMultipartFile' to required type 'com.terminal.domain.validation.OnlyForImagesFileWrapper'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type [com.terminal.domain.validation.OnlyForImagesFileWrapper]: no matching editors or conversion strategy found
        at           org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:74)
        ....
        Caused by: java.lang.IllegalStateException: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type [com.terminal.domain.validation.OnlyForImagesFileWrapper]: no matching editors or conversion strategy found
            at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267)
            ... 71 more

OnlyForImagesFileWrapper source:

public class OnlyForImagesFileWrapper {
    @Extensions(imageFormats = {".jpg",".png",".gif",".bmp"}, videoFormats = {})
    private MultipartFile multipartFile;
    ...
}

How to avoid the problem?

Where can I set conversion politic for this controller method for multipart file?

P.S.

I tryed to write my custom initbinder:

@InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(CommonsMultipartFile.class, new PropertyEditorSupport() {
            @Override
            public void setValue(Object file) {
                setValue(new OnlyForImagesFileWrapper((MultipartFile) file));
            }
        });
    }

But this method doesn't invoke when I submit form and I see stacktrace mentioned above.

P.S.

result after M. Deinum instruction execution(when I inside saveTerminal method):

http://dl2.joxi.net/drive/0005/3037/338909/150502/e592df25e4.jpg

Also I noticed that my initbinder method doesn't invoke.

More details about my code(state after M. Denium advice):

jsp:

<input type="file" id="newFile" name="file" class="file" size="21.5" accept=".jpg,.png,.gif,.bmp" style="opacity: 0;">

arguments of controller method:

...
@ModelAttribute @Valid OnlyForImagesFileWrapper wrapper,
                               BindingResult bindingResult,
...

Solution

  • As I commented you are making things too complex. Change your wrapper to the following (with the appropriate getters and setters).

    public class OnlyForImagesFileWrapper {
        @Extensions(imageFormats = {".jpg",".png",".gif",".bmp"}, videoFormats = {})
        private MultipartFile file;
        private String name;
    ...
    }
    

    Then your controller method

    @RequestMapping(value = "/owner/terminals/save", method = RequestMethod.POST)
    public String saveTerminal( @ModelAttribute @Valid OnlyForImagesFileWrapper wrapper, BindingResult bindingResult ) { ... }
    

    And of course in your configuration make sure you have a MultipartFileResolver configured to properly handle the MultipartFile argument as explained in the reference guide.