From b32cd7a635d0668fedaf7a204d635880b9565d8c Mon Sep 17 00:00:00 2001 From: Yasin Bhojawala Date: Sat, 4 Nov 2017 18:12:45 +0530 Subject: [PATCH] Add example for implicit conversion in controller methods --- .../StringToEmployeeConverterController.java | 16 +++++++++ ...ringToEmployeeConverterControllerTest.java | 34 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 spring-boot/src/main/java/org/baeldung/converter/controller/StringToEmployeeConverterController.java create mode 100644 spring-boot/src/test/java/org/baeldung/converter/controller/StringToEmployeeConverterControllerTest.java diff --git a/spring-boot/src/main/java/org/baeldung/converter/controller/StringToEmployeeConverterController.java b/spring-boot/src/main/java/org/baeldung/converter/controller/StringToEmployeeConverterController.java new file mode 100644 index 0000000000..edd56dc136 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/converter/controller/StringToEmployeeConverterController.java @@ -0,0 +1,16 @@ +package org.baeldung.converter.controller; + +import com.baeldung.toggle.Employee; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/string-to-employee") +public class StringToEmployeeConverterController { + + @GetMapping + public ResponseEntity getStringToEmployee(@RequestParam("employee") Employee employee) { + return ResponseEntity.ok(employee); + } +} diff --git a/spring-boot/src/test/java/org/baeldung/converter/controller/StringToEmployeeConverterControllerTest.java b/spring-boot/src/test/java/org/baeldung/converter/controller/StringToEmployeeConverterControllerTest.java new file mode 100644 index 0000000000..a5ed7998b4 --- /dev/null +++ b/spring-boot/src/test/java/org/baeldung/converter/controller/StringToEmployeeConverterControllerTest.java @@ -0,0 +1,34 @@ +package org.baeldung.converter.controller; + +import org.baeldung.Application; +import org.junit.Test; +import org.junit.runner.RunWith; +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.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +import static org.hamcrest.CoreMatchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class) +@AutoConfigureMockMvc +public class StringToEmployeeConverterControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void getStringToEmployeeTest() throws Exception { + mockMvc.perform(get("/string-to-employee?employee=1,2000")) + .andDo(print()) + .andExpect(jsonPath("$.id", is(1))) + .andExpect(jsonPath("$.salary", is(2000.0))) + .andExpect(status().isOk()); + } +}