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 index 9ad687d758..4af09968fa 100644 --- 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 @@ -8,10 +8,10 @@ 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") +@RequestMapping(value = "/", produces = "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") + @RequestMapping(value = "/public/api/endpoint", produces = "application/vnd.baeldung.api.v1+json") public @ResponseBody ResponseEntity getItem() { return new ResponseEntity<>(new BaeldungItem("itemId1"), HttpStatus.OK); } diff --git a/spring-rest/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerLiveTest.java b/spring-rest/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerLiveTest.java new file mode 100644 index 0000000000..befb851a2c --- /dev/null +++ b/spring-rest/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerLiveTest.java @@ -0,0 +1,33 @@ +package org.baeldung.web.controller.mediatypes; + +import com.jayway.restassured.RestAssured; +import com.jayway.restassured.response.Response; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = {TestConfig.class}, loader = AnnotationConfigContextLoader.class) +public class CustomMediaTypeControllerLiveTest { + private static final String URL_PREFIX = "http://localhost:8082/spring-rest"; + private static final String CUSTOM_CONTENT_TYPE = "application/vnd.baeldung.api.v1+json"; + + @Test + public void getFor_customMediaType() { + //given + String url = URL_PREFIX + "/public/api/endpoint"; + + //when + Response response = RestAssured.get(url); + + //then + assertEquals(200, response.getStatusCode()); + assertTrue(response.contentType().contains(CUSTOM_CONTENT_TYPE)); + + } +} 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 index a0a51e935d..3fa2d5678c 100644 --- 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 @@ -32,6 +32,6 @@ public class CustomMediaTypeControllerTest { @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()); + mockMvc.perform(get("/public/api/endpoint").accept("application/vnd.baeldung.api.v1+json")).andExpect(status().isOk()); } } \ No newline at end of file diff --git a/spring-rest/src/test/java/org/baeldung/web/controller/mediatypes/TestConfig.java b/spring-rest/src/test/java/org/baeldung/web/controller/mediatypes/TestConfig.java new file mode 100644 index 0000000000..66ffe4947d --- /dev/null +++ b/spring-rest/src/test/java/org/baeldung/web/controller/mediatypes/TestConfig.java @@ -0,0 +1,11 @@ +package org.baeldung.web.controller.mediatypes; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + + +@Configuration +@ComponentScan({ "org.baeldung.web" }) +public class TestConfig { + +}