Skip to content

Instantly share code, notes, and snippets.

@adiberr
Created July 22, 2024 21:05
Show Gist options
  • Select an option

  • Save adiberr/1b70ea2817dc005defb99d84b9d31a3a to your computer and use it in GitHub Desktop.

Select an option

Save adiberr/1b70ea2817dc005defb99d84b9d31a3a to your computer and use it in GitHub Desktop.
File upload example with Swagger UI - Spring Boot 2.7
@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