Skip to content
Advertisement

Error while trying to upload file conversion types mismatch

I have folowing entety.

JavaScript

One of the fields called file is to upload the document. And the controller function for this is as follows:

JavaScript

But then I try to upload file and submit a form I am getting this exception Failed to convert property value of type ‘org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile’ to required type ‘java.lang.String’ for property ‘file’; nested exception is java.lang.IllegalStateException: Cannot convert value of type ‘org.springframewo

Full error is:

JavaScript

Advertisement

Answer

@ModelAttribute will enable data binding from the HTTP request to @ModelAttribute instance.

It will try to bind the value from the query parameter , field name of the form data and others (see this) to the @ModelAttribute instance ‘s field provided that their names are matched.

In your case , as your controller method has a query parameter called file , it will try to bind its value to the Documents ‘s file field. As the file query parameter is in MultipartFile type but Documents is in String type , it requires conversion but there are no Converter registered for doing it.

Not sure if you really want to bind the content of the MultipartFile to the Documents ‘s file field. If not, you can simply rename either one such that they are not matched.

Otherwise, you can implement a Converter to convert MultipartFile to String :

JavaScript

And register it :

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement