Created
July 22, 2024 21:05
-
-
Save adiberr/1b70ea2817dc005defb99d84b9d31a3a to your computer and use it in GitHub Desktop.
File upload example with Swagger UI - Spring Boot 2.7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @SpringBootApplication | |
| public class App { | |
| public static void main(String[] args) { | |
| SpringApplication.run(App.class, args); | |
| } | |
| } | |
| @RestController | |
| class UploadController { | |
| @Operation(summary = "Upload a file", description = "Uploads a file to the server") | |
| @ApiResponses(value = { | |
| @ApiResponse(responseCode = "200", description = "File uploaded successfully"), | |
| @ApiResponse(responseCode = "400", description = "Invalid file") | |
| }) | |
| @PostMapping(path = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) | |
| public ResponseEntity<String> upload( | |
| @Parameter(description = "File name") | |
| @RequestParam String filename, | |
| @Parameter(description = "File to be uploaded") | |
| @RequestPart MultipartFile file) { | |
| if (file.isEmpty()) { | |
| return ResponseEntity.badRequest().body("File is empty"); | |
| } | |
| return ResponseEntity.ok("File uploaded successfully"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment