Multipart File upload using Springfox and Swagger-ui
Asked Answered
H

7

32

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.

Hoem answered 15/7, 2015 at 1:45 Comment(1)
It would be nice if you set the correct answer.Extramural
R
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<>();
}
Ranzini answered 27/11, 2017 at 21:8 Comment(0)
F
15

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

Franglais answered 26/7, 2015 at 0:33 Comment(0)
P
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();
}
Ponder answered 22/4, 2016 at 1:37 Comment(2)
Thank you for your code snippet.<br/> I've used the solution from SO link, 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 library (at least I had to), otherwise Spring IoC system could not retrieve a bean to delegate file upload to.Alicyclic
in my case, I had to change @ RequestParam to @ RequestPart like the original answer.maybe you should edit answer. tyRetrusion
G
10

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
    }

}
Gitlow answered 4/12, 2020 at 12:39 Comment(1)
This does answer does not add anything that hasnt already been provided by previous answersDevoirs
L
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

Leahy answered 27/7, 2017 at 17:36 Comment(0)
E
0

enter image description here

After trying all of above solution, I still have 1 point that can't rendering the Multipart File in Swagger open ai. I found the solution by a little bit change in Controller class:

  • Use @ApiParam and @RequestPart.
  • Use @RequestMapping instead of @PostMapping. I have no idea why @RequestMapping works instead of @PostMapping. My workspace's using below dependencies:
org.springframework.boot spring-boot-starter-web
org.springdoc springdoc-openapi-starter-webmvc-ui 2.3.0
Endemic answered 9/3, 2024 at 13:52 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Lascar
Why should I not upload images of code/data/errors?Revocation
H
0

Springfox is great, but since the project is deprecated/abandoned you may end up here using Springdoc. I know I did, yet I pretty much had the same problem. Here is all the things I needed to make this work:

In the controller:

@PostMapping(consumes = MULTIPART_FORM_DATA_VALUE)
@ResponseStatus(CREATED)
public ReportResponseDTO requestReport(
        @Parameter(schema = @Schema(implementation = ReportRequestDTO.class))
        @RequestPart(value = "request") @Valid ReportRequestDTO request,
        @RequestPart(value = "file") MultipartFile file) {
    // stuff
}

This will show up the request in the Swagger UI as the correct example payload, based on ReportRequestDTO, and the file as a file upload button.

To make this work in its entirety, I had to add a converter, as shown here:

@Component
public class MediaTypeOctetStreamConverter extends AbstractJackson2HttpMessageConverter {
    public MediaTypeOctetStreamConverter(ObjectMapper objectMapper) {
        super(objectMapper, MediaType.APPLICATION_OCTET_STREAM);
    }
}
Hatshepsut answered 15/7, 2024 at 5:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.