diff --git a/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/postman/PostmanUploadDemo.java b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/postman/PostmanUploadDemo.java new file mode 100644 index 0000000000..b3721fefe4 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/postman/PostmanUploadDemo.java @@ -0,0 +1,15 @@ +package com.baeldung.postman; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +@SpringBootApplication +@EnableWebMvc +public class PostmanUploadDemo { + + public static void main(String[] args) { + SpringApplication.run(PostmanUploadDemo.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/postman/controller/PostmanUploadController.java b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/postman/controller/PostmanUploadController.java new file mode 100644 index 0000000000..3ee5cf28e5 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/postman/controller/PostmanUploadController.java @@ -0,0 +1,24 @@ +package com.baeldung.postman.controller; + +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; + +import com.baeldung.postman.model.JsonRequest; + +@Controller +public class PostmanUploadController { + + @PostMapping("/uploadFile") + public ResponseEntity handleFileUpload(@RequestParam("file") MultipartFile file){ + return ResponseEntity.ok().body("file received successfully"); + } + + @PostMapping("/uploadJson") + public ResponseEntity handleJsonInput(@RequestBody JsonRequest json){ + return ResponseEntity.ok().body(json.getId()+json.getName()); + } +} diff --git a/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/postman/model/JsonRequest.java b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/postman/model/JsonRequest.java new file mode 100644 index 0000000000..326f32c30b --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/postman/model/JsonRequest.java @@ -0,0 +1,30 @@ +package com.baeldung.postman.model; + +public class JsonRequest { + int id; + String name; + + public JsonRequest() { + } + + public JsonRequest(int id, String name) { + this.id = id; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/spring-boot-modules/spring-boot-mvc-4/src/test/java/com/baeldung/postman/controller/PostmanUploadControllerTest.java b/spring-boot-modules/spring-boot-mvc-4/src/test/java/com/baeldung/postman/controller/PostmanUploadControllerTest.java new file mode 100644 index 0000000000..bf010f50e8 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-4/src/test/java/com/baeldung/postman/controller/PostmanUploadControllerTest.java @@ -0,0 +1,43 @@ +package com.baeldung.postman.controller; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import com.baeldung.postman.PostmanUploadDemo; +import com.baeldung.postman.model.JsonRequest; +import com.fasterxml.jackson.databind.ObjectMapper; + +@SpringBootTest(classes = PostmanUploadDemo.class) +@AutoConfigureMockMvc +public class PostmanUploadControllerTest { + @Autowired + private MockMvc mockMvc; + + @Test + public void givenJson_whenUploaded_thenSuccessReturned() throws Exception { + JsonRequest request = new JsonRequest(1, "John Doe"); + this.mockMvc.perform(MockMvcRequestBuilders.post("/uploadJson") + .contentType(MediaType.APPLICATION_JSON) + .content(new ObjectMapper().writeValueAsString(request))) + .andExpect(status().isOk()) + .andExpect(content().string("1John Doe")); + } + + @Test + public void givenFile_whenUploaded_thenSuccessReturned() throws Exception { + MockMultipartFile request = new MockMultipartFile("dummy", "{\"key\": \"value\"}".getBytes()); + this.mockMvc.perform(MockMvcRequestBuilders.multipart("/uploadFile") + .file("file", request.getBytes())) + .andExpect(status().isOk()) + .andExpect(content().string("file received successfully")); + } +}