简体   繁体   中英

Multipart File upload using Springfox and Swagger-ui

I'm using Spring MVC as a rest controller and I've integrated Swagger-ui with my controller using Springfox. I'd like to have a method that is able to upload a file via the Swagger-ui interface. I only need two parameters, a long acting for an object id and the file to be uploaded.

@RestController
public class controller{
    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public void uploadFile(@RequestParam Long id,
                           @RequestParam MultipartFile file){
          //do some stuff
    }
}

I've tried almost everything and I can't get a file upload button to appear. However, if I do:

@RestController
public class Controller{
    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public void uploadFile(@RequestParam Long id,
                           @RequestPart File file){
         //do some stuff
    }
}

The file upload button appears, but it always throws http code 415 when trying to upload a file. Besides, I need the input to be a MultipartFile, not a regular File. Even if I use the @RequestPart annotation with Multipart File, the choose file to upload button does not appear. How can I get this to work???? Even:

@RestController
public class Controller{
    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public void uploadFile(@RequestPart String metaData,
                           @RequestPart MultipartFile file){
        //do some stuff
    }
}

Won't work. If someone could give a walkthrough of how to get this button to appear for MultipartFile? I'd be forever grateful.

Use

@RequestPart(required = true) MultipartFile file

And use the version number 2.1.0 or latest, there is a bug with previous versions.

https://github.com/springfox/springfox/issues/786

In my situation, there were two things I needed to do

  1. My MultipartFile request param had to be named 'file', otherwise, swagger-ui wouldn't display the file upload input control
@RequestParam("file") MultipartFile file
  1. I had to register the following bean
 @Bean(name = "multipartResolver") public CommonsMultipartResolver commonsMultipartResolver(){ return new CommonsMultipartResolver(); }

I think you are missing the consumes attribute of the @RequestMapping in your second snippet. See the following example

@RequestMapping(
    path = "/upload", 
    method = RequestMethod.POST, 
    consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> handleUpload(
        @RequestPart("file") MultipartFile file, 
        @RequestParam("someId") Long someId,         
        @RequestParam("someOtherId") Long someOtherId) { 
    return new ResponseEntity<>();
}

Two things...

  1. Value of consumes should should be "multipart/form-data" . consumes="multipart/form-data"

  2. @RequestPart("file") @ApiParam(value="File", required=true) MultipartFile file

Try using @RequestPart for MultipartFile instead of @RequestParam

@RestController
public class controller {

    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public void uploadFile(@RequestParam Long id,
                       @RequestPart MultipartFile file) {
        //do some stuff
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM