31

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.

drumlord
  • 311
  • 1
  • 3
  • 4

5 Answers5

28

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<>();
}
abedurftig
  • 1,118
  • 1
  • 12
  • 24
14

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

Benn Sandoval
  • 873
  • 7
  • 17
12

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();
}
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
ashario
  • 2,694
  • 1
  • 23
  • 19
  • 1
    Thank you for your code snippet.
    I've used the solution from [SO link](http://stackoverflow.com/a/32992815/3122531), but it implied to use XML config for Spring, which I was trying to avoid :) It should be pointed that, when using Maven/Gradle/etc, you have to add dependency for [Apache Commons FileUpload](https://commons.apache.org/proper/commons-fileupload/) library (at least I had to), otherwise Spring IoC system could not retrieve a bean to delegate file upload to.
    – mginius Jan 05 '17 at 10:52
  • 3
    in my case, I had to change @ RequestParam to @ RequestPart like the original answer.maybe you should edit answer. ty – Hosseinmp76 Dec 27 '20 at 12:01
9

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
    }

}
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
6

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

TuGordoBello
  • 4,350
  • 9
  • 52
  • 78