Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,18 +17,28 @@ public class PostmanUploadController {
@PostMapping("/uploadFile")
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
return ResponseEntity.ok()
.body("file received successfully");
.body("file received successfully");
}

@PostMapping("/uploadJson")
public ResponseEntity<String> handleJsonInput(@RequestBody JsonRequest json) {
return ResponseEntity.ok()
.body(json.getId() + json.getName());
.body(json.getId() + json.getName());
}

@PostMapping("/uploadJsonAndMultipartData")
public ResponseEntity<String> 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<String> handleSingleFileUpload(@RequestParam("file") MultipartFile file) {
return ResponseEntity.ok("file received successfully");
}

@PostMapping(value = "/uploadJsonAndMultipartInput", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> handleUploadJsonAndMultipartInput(@RequestPart("data") JsonRequest json, @RequestPart("file") MultipartFile file) {
return ResponseEntity.ok(json.getId() + json.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

}
}