Skip to content

Commit 151a48e

Browse files
authored
Merge pull request #18921 from wynnteo/BAEL-9468
BAEL-9468
2 parents 1983315 + 3cb7e55 commit 151a48e

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/postman/controller/PostmanUploadController.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baeldung.postman.controller;
22

3+
import org.springframework.http.MediaType;
34
import org.springframework.http.ResponseEntity;
45
import org.springframework.stereotype.Controller;
56
import org.springframework.web.bind.annotation.PostMapping;
@@ -16,18 +17,28 @@ public class PostmanUploadController {
1617
@PostMapping("/uploadFile")
1718
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
1819
return ResponseEntity.ok()
19-
.body("file received successfully");
20+
.body("file received successfully");
2021
}
2122

2223
@PostMapping("/uploadJson")
2324
public ResponseEntity<String> handleJsonInput(@RequestBody JsonRequest json) {
2425
return ResponseEntity.ok()
25-
.body(json.getId() + json.getName());
26+
.body(json.getId() + json.getName());
2627
}
2728

2829
@PostMapping("/uploadJsonAndMultipartData")
2930
public ResponseEntity<String> handleJsonAndMultipartInput(@RequestPart("data") JsonRequest json, @RequestPart("file") MultipartFile file) {
3031
return ResponseEntity.ok()
31-
.body(json.getId() + json.getName());
32+
.body(json.getId() + json.getName());
33+
}
34+
35+
@PostMapping(value = "/uploadSingleFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
36+
public ResponseEntity<String> handleSingleFileUpload(@RequestParam("file") MultipartFile file) {
37+
return ResponseEntity.ok("file received successfully");
38+
}
39+
40+
@PostMapping(value = "/uploadJsonAndMultipartInput", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
41+
public ResponseEntity<String> handleUploadJsonAndMultipartInput(@RequestPart("data") JsonRequest json, @RequestPart("file") MultipartFile file) {
42+
return ResponseEntity.ok(json.getId() + json.getName());
3243
}
3344
}

spring-boot-modules/spring-boot-mvc-2/src/test/java/com/baeldung/postman/controller/PostmanUploadControllerUnitTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,27 @@ public void givenFile_whenUploaded_thenSuccessReturned() throws Exception {
4040
.andExpect(status().isOk())
4141
.andExpect(content().string("file received successfully"));
4242
}
43+
44+
@Test
45+
public void givenFile_whenUploadSingleFile_thenSuccessReturned() throws Exception {
46+
MockMultipartFile request = new MockMultipartFile("dummy", "{\"key\": \"value\"}".getBytes());
47+
this.mockMvc.perform(MockMvcRequestBuilders.multipart("/uploadSingleFile")
48+
.file("file", request.getBytes()))
49+
.andExpect(status().isOk())
50+
.andExpect(content().string("file received successfully"));
51+
}
52+
53+
@Test
54+
public void givenJsonAndFile_whenUploadJsonAndMultipart_thenSuccessReturned() throws Exception {
55+
String jsonString = "{\"id\": 1, \"name\": \"Alice\"}";
56+
MockMultipartFile jsonPart = new MockMultipartFile("data", "", "application/json", jsonString.getBytes());
57+
MockMultipartFile filePart = new MockMultipartFile("file", "test.txt", "text/plain", "some file content".getBytes());
58+
59+
this.mockMvc.perform(MockMvcRequestBuilders.multipart("/uploadJsonAndMultipartInput")
60+
.file(jsonPart)
61+
.file(filePart))
62+
.andExpect(status().isOk())
63+
.andExpect(content().string("1Alice"));
64+
65+
}
4366
}

0 commit comments

Comments
 (0)