From 4305025d42be02d9ed17b31dbe17a8cb9be2644a Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Fri, 30 Dec 2016 20:14:25 +0100 Subject: [PATCH] Custom media types rest (#944) * Custom Media Types for REST * add test, add DTO, remove example of API versioning to make example simpler * client accept only that custom-media type * remove not needed new_line * leave custom media type on class level * do not need content type header * GET do not need content-Type * add liveTest for custom Content type * Merge branch 'master' of https://github.com/eugenp/tutorials into Custom_media_types_rest # Conflicts: # spring-rest/src/main/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeController.java # spring-rest/src/main/java/org/baeldung/web/dto/BaeldungItem.java # spring-rest/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerTest.java * test name proper given_when_then pattern * add custom content-type 'application/json-p' * import proper class * proper test name * proper API migraton test, SecondBaeldungItem breaks backward compatibility --- .../java/org/baeldung/config/WebConfig.java | 6 +-- .../mediatypes/CustomMediaTypeController.java | 15 +++++--- .../web/dto/BaeldungItemSecondVersion.java | 14 +++++++ .../CustomMediaTypeControllerLiveTest.java | 37 ++++++++++--------- .../CustomMediaTypeControllerTest.java | 7 +++- .../web/controller/mediatypes/TestConfig.java | 1 + 6 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 spring-rest/src/main/java/org/baeldung/web/dto/BaeldungItemSecondVersion.java diff --git a/spring-rest/src/main/java/org/baeldung/config/WebConfig.java b/spring-rest/src/main/java/org/baeldung/config/WebConfig.java index d04214c377..f40c9477d4 100644 --- a/spring-rest/src/main/java/org/baeldung/config/WebConfig.java +++ b/spring-rest/src/main/java/org/baeldung/config/WebConfig.java @@ -1,8 +1,5 @@ package org.baeldung.config; -import java.text.SimpleDateFormat; -import java.util.List; - import org.baeldung.config.converter.KryoHttpMessageConverter; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -15,6 +12,9 @@ import org.springframework.oxm.xstream.XStreamMarshaller; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import java.text.SimpleDateFormat; +import java.util.List; + /* * Please note that main web configuration is in src/main/webapp/WEB-INF/api-servlet.xml */ 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 4af09968fa..72affe2e91 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 @@ -1,9 +1,9 @@ package org.baeldung.web.controller.mediatypes; import org.baeldung.web.dto.BaeldungItem; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; +import org.baeldung.web.dto.BaeldungItemSecondVersion; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @@ -11,8 +11,13 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping(value = "/", produces = "application/vnd.baeldung.api.v1+json") public class CustomMediaTypeController { - @RequestMapping(value = "/public/api/endpoint", produces = "application/vnd.baeldung.api.v1+json") - public @ResponseBody ResponseEntity getItem() { - return new ResponseEntity<>(new BaeldungItem("itemId1"), HttpStatus.OK); + @RequestMapping(method = RequestMethod.GET, value = "/public/api/endpoint", produces = "application/vnd.baeldung.api.v1+json") + public @ResponseBody BaeldungItem getItem() { + return new BaeldungItem("itemId1"); + } + + @RequestMapping(method = RequestMethod.GET, value = "/public/api/endpoint", produces = "application/vnd.baeldung.api.v2+json") + public @ResponseBody BaeldungItemSecondVersion getItemSecondAPIVersion() { + return new BaeldungItemSecondVersion("itemName"); } } diff --git a/spring-rest/src/main/java/org/baeldung/web/dto/BaeldungItemSecondVersion.java b/spring-rest/src/main/java/org/baeldung/web/dto/BaeldungItemSecondVersion.java new file mode 100644 index 0000000000..d4af3cf0e9 --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/web/dto/BaeldungItemSecondVersion.java @@ -0,0 +1,14 @@ +package org.baeldung.web.dto; + + +public class BaeldungItemSecondVersion { + private final String itemName; + + public BaeldungItemSecondVersion(String itemName) { + this.itemName = itemName; + } + + public String getItemName() { + return itemName; + } +} 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 index 8eb2a96593..81348c07a3 100644 --- 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 @@ -1,36 +1,37 @@ package org.baeldung.web.controller.mediatypes; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - +import com.jayway.restassured.http.ContentType; 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 com.jayway.restassured.RestAssured; -import com.jayway.restassured.response.Response; +import static com.jayway.restassured.RestAssured.given; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.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 whenGet_receiveCustomMediaType() { - // given - String url = URL_PREFIX + "/public/api/endpoint"; + public void givenServiceEndpoint_whenGetRequestFirstAPIVersion_thenShouldReturn200() { + given() + .accept("application/vnd.baeldung.api.v1+json") + .when() + .get(URL_PREFIX + "/public/api/endpoint") + .then() + .contentType(ContentType.JSON).and().statusCode(200); + } - // when - Response response = RestAssured.get(url); - - // then - assertEquals(200, response.getStatusCode()); - assertTrue(response.contentType().contains(CUSTOM_CONTENT_TYPE)); + @Test + public void givenServiceEndpoint_whenGetRequestSecondAPIVersion_thenShouldReturn200() { + given() + .accept("application/vnd.baeldung.api.v2+json") + .when() + .get(URL_PREFIX + "/public/api/endpoint") + .then() + .contentType(ContentType.JSON).and().statusCode(200); } } 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 3fa2d5678c..e5f44e58a7 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 @@ -31,7 +31,12 @@ public class CustomMediaTypeControllerTest { } @Test - public void shouldSendRequestForItem() throws Exception { + public void givenServiceUrl_whenGetWithProperAcceptHeaderFirstAPIVersion_thenReturn200() throws Exception { mockMvc.perform(get("/public/api/endpoint").accept("application/vnd.baeldung.api.v1+json")).andExpect(status().isOk()); } + + @Test + public void givenServiceUrl_whenGetWithProperAcceptHeaderSecondVersion_thenReturn200() throws Exception { + mockMvc.perform(get("/public/api/endpoint").accept("application/vnd.baeldung.api.v2+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 index 340e0284af..66ffe4947d 100644 --- 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 @@ -3,6 +3,7 @@ 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 {