The backend interface encapsulates @RequestBody and submits the file upload at the same time

When the back-end interface needs to transfer files (MultipartFile) and data, there will always be request failures. Later, I found that the data I uploaded uses the @RequestBody annotation, and the corresponding type of the @RequestBody annotation is application/json, file upload The annotation uses the @MultipartFile annotation, and the type corresponding to the file MultipartFile is multipart/form-data. These two formats are not compatible, so no matter how you write it, the call fails. 

request result 

 Solution:

Cancel the @RequestBody annotation, and encapsulate the MultipartFile file attribute field request in the vo class, which is the content uploaded by the entire front end.

 The interface is encapsulated into a vo parameter, do not add @RequestBody annotation, the following is the interface display of the controller layer:

         Then you must pay attention here, you must not use swagger to test, swagger does not support multi-file upload interface debugging, and always reports an error, this is a pit, and the blogger has personally experienced it. It is recommended to use postman to test. Of course, other tools are also available. Also, pay attention to the three places on the picture, edit the content, set it to text format, set the parameters to be uploaded, select the file upload type as file, and add the file to be uploaded .

 

 

Guess you like

Origin blog.csdn.net/qq_56728342/article/details/125700856