spring-bootspring-mvcmultipartform-dataswagger-ui

How to upload file in swagger in spring boot application


I want to upload a file within Swagger UI of my project, the controller method should accept an object which contains a Long and a Multipart file, how should I do that?

This is request object:

@AllArgsConstructor
@Getter
public class BankFileUploadRequest {

    @NotNull(message = "{validation.bankFileRequestModel.bankAccountId.null}")
    private final Long bankAccountId;
    @NotNull(message = "{validation.bankFileRequestModel.file.null}")
    private final MultipartFile file;

}

and besides that I have a custom Spring validator which validates BankFileUploadRequest, and I want to have a controller method as:

@PostMapping(path = "/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public void uploadFile(@Validated BankFileUploadRequest request) {
    ...
}

what kind of annotations should I add to request argument, and how to have file upload support in swagger?


Solution

  • Adding @ModelAttribute to the parameter solved the issue, now both Swagger supports the file upload, and it works, so the signature of controller method is:

    @PostMapping(path = "/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public void uploadFile(@ModelAttribute("request") @Validated BankFileUploadRequest request) {
        ....
    }