diff --git a/spring-rest/src/main/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeController.java b/spring-rest/src/main/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeController.java new file mode 100644 index 0000000000..d5a64babfe --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeController.java @@ -0,0 +1,21 @@ +package org.baeldung.web.controller.mediatypes; + + +import org.baeldung.web.dto.BaeldungItem; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(value = "/", produces = "application/vnd.baeldung.api.v1+json", + consumes = "application/vnd.baeldung.api.v1+json") +public class CustomMediaTypeController { + + @RequestMapping(value = "/public/api/endpoint", produces = "application/vnd.baeldung.api.v1+json", + consumes = "application/vnd.baeldung.api.v1+json") + public @ResponseBody ResponseEntity getItem() { + return new ResponseEntity<>(new BaeldungItem("itemId1"), HttpStatus.OK); + } +} diff --git a/spring-rest/src/main/java/org/baeldung/web/dto/BaeldungItem.java b/spring-rest/src/main/java/org/baeldung/web/dto/BaeldungItem.java new file mode 100644 index 0000000000..9053395302 --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/dto/BaeldungItem.java @@ -0,0 +1,14 @@ +package org.baeldung.web.dto; + + +public class BaeldungItem { + private final String itemId; + + public BaeldungItem(String itemId) { + this.itemId = itemId; + } + + public String getItemId() { + return itemId; + } +} diff --git a/spring-rest/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerTest.java b/spring-rest/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerTest.java new file mode 100644 index 0000000000..5b494cac34 --- /dev/null +++ b/spring-rest/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerTest.java @@ -0,0 +1,41 @@ +package org.baeldung.web.controller.mediatypes; + +import org.baeldung.config.WebConfig; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = WebConfig.class) +@WebAppConfiguration +public class CustomMediaTypeControllerTest { + + private MockMvc mockMvc; + + @Autowired + private WebApplicationContext webApplicationContext; + + @Before + public void setUp() { + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); + } + + @Test + public void shouldSendRequestForItem() throws Exception { + mockMvc.perform(get("/public/api/endpoint") + .contentType("application/vnd.baeldung.api.v1+json") + .accept("application/vnd.baeldung.api.v1+json")) + .andExpect(status().isOk()); + } +} \ No newline at end of file