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

喜欢而已 提交于 2020-01-11 09:42:09

问题


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):

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,
...

回答1:


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.



来源:https://stackoverflow.com/questions/30005724/failed-to-convert-value-of-type-org-springframework-web-multipart-commons-commo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!