diff --git a/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/postman/controller/PostmanUploadController.java b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/postman/controller/PostmanUploadController.java index 6225a6b34b4e..d33f9f89724c 100644 --- a/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/postman/controller/PostmanUploadController.java +++ b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/postman/controller/PostmanUploadController.java @@ -1,5 +1,6 @@ package com.baeldung.postman.controller; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; @@ -16,18 +17,28 @@ public class PostmanUploadController { @PostMapping("/uploadFile") public ResponseEntity handleFileUpload(@RequestParam("file") MultipartFile file) { return ResponseEntity.ok() - .body("file received successfully"); + .body("file received successfully"); } @PostMapping("/uploadJson") public ResponseEntity handleJsonInput(@RequestBody JsonRequest json) { return ResponseEntity.ok() - .body(json.getId() + json.getName()); + .body(json.getId() + json.getName()); } @PostMapping("/uploadJsonAndMultipartData") public ResponseEntity handleJsonAndMultipartInput(@RequestPart("data") JsonRequest json, @RequestPart("file") MultipartFile file) { return ResponseEntity.ok() - .body(json.getId() + json.getName()); + .body(json.getId() + json.getName()); + } + + @PostMapping(value = "/uploadSingleFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity handleSingleFileUpload(@RequestParam("file") MultipartFile file) { + return ResponseEntity.ok("file received successfully"); + } + + @PostMapping(value = "/uploadJsonAndMultipartInput", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity handleUploadJsonAndMultipartInput(@RequestPart("data") JsonRequest json, @RequestPart("file") MultipartFile file) { + return ResponseEntity.ok(json.getId() + json.getName()); } } diff --git a/spring-boot-modules/spring-boot-mvc-2/src/test/java/com/baeldung/postman/controller/PostmanUploadControllerUnitTest.java b/spring-boot-modules/spring-boot-mvc-2/src/test/java/com/baeldung/postman/controller/PostmanUploadControllerUnitTest.java index 1049d2ec0d15..e0da3b45f342 100644 --- a/spring-boot-modules/spring-boot-mvc-2/src/test/java/com/baeldung/postman/controller/PostmanUploadControllerUnitTest.java +++ b/spring-boot-modules/spring-boot-mvc-2/src/test/java/com/baeldung/postman/controller/PostmanUploadControllerUnitTest.java @@ -40,4 +40,27 @@ public void givenFile_whenUploaded_thenSuccessReturned() throws Exception { .andExpect(status().isOk()) .andExpect(content().string("file received successfully")); } + + @Test + public void givenFile_whenUploadSingleFile_thenSuccessReturned() throws Exception { + MockMultipartFile request = new MockMultipartFile("dummy", "{\"key\": \"value\"}".getBytes()); + this.mockMvc.perform(MockMvcRequestBuilders.multipart("/uploadSingleFile") + .file("file", request.getBytes())) + .andExpect(status().isOk()) + .andExpect(content().string("file received successfully")); + } + + @Test + public void givenJsonAndFile_whenUploadJsonAndMultipart_thenSuccessReturned() throws Exception { + String jsonString = "{\"id\": 1, \"name\": \"Alice\"}"; + MockMultipartFile jsonPart = new MockMultipartFile("data", "", "application/json", jsonString.getBytes()); + MockMultipartFile filePart = new MockMultipartFile("file", "test.txt", "text/plain", "some file content".getBytes()); + + this.mockMvc.perform(MockMvcRequestBuilders.multipart("/uploadJsonAndMultipartInput") + .file(jsonPart) + .file(filePart)) + .andExpect(status().isOk()) + .andExpect(content().string("1Alice")); + + } }