Custom media types rest (#946)
* 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 * add proper endpoint
This commit is contained in:
parent
4305025d42
commit
4d8949f659
@ -1,23 +1,20 @@
|
||||
package org.baeldung.web.controller.mediatypes;
|
||||
|
||||
import org.baeldung.web.dto.BaeldungItem;
|
||||
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;
|
||||
import org.baeldung.web.dto.BaeldungItemV2;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/", produces = "application/vnd.baeldung.api.v1+json")
|
||||
public class CustomMediaTypeController {
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/public/api/endpoint", produces = "application/vnd.baeldung.api.v1+json")
|
||||
public @ResponseBody BaeldungItem getItem() {
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/public/api/items/{id}", produces = "application/vnd.baeldung.api.v1+json")
|
||||
public @ResponseBody BaeldungItem getItem(@PathVariable("id") String id) {
|
||||
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");
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/public/api/items/{id}", produces = "application/vnd.baeldung.api.v2+json")
|
||||
public @ResponseBody BaeldungItemV2 getItemSecondAPIVersion(@PathVariable("id") String id) {
|
||||
return new BaeldungItemV2("itemName");
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package org.baeldung.web.dto;
|
||||
|
||||
|
||||
public class BaeldungItemSecondVersion {
|
||||
public class BaeldungItemV2 {
|
||||
private final String itemName;
|
||||
|
||||
public BaeldungItemSecondVersion(String itemName) {
|
||||
public BaeldungItemV2(String itemName) {
|
||||
this.itemName = itemName;
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ public class CustomMediaTypeControllerLiveTest {
|
||||
given()
|
||||
.accept("application/vnd.baeldung.api.v1+json")
|
||||
.when()
|
||||
.get(URL_PREFIX + "/public/api/endpoint")
|
||||
.get(URL_PREFIX + "/public/api/items/1")
|
||||
.then()
|
||||
.contentType(ContentType.JSON).and().statusCode(200);
|
||||
}
|
||||
@ -30,7 +30,7 @@ public class CustomMediaTypeControllerLiveTest {
|
||||
given()
|
||||
.accept("application/vnd.baeldung.api.v2+json")
|
||||
.when()
|
||||
.get(URL_PREFIX + "/public/api/endpoint")
|
||||
.get(URL_PREFIX + "/public/api/items/2")
|
||||
.then()
|
||||
.contentType(ContentType.JSON).and().statusCode(200);
|
||||
}
|
||||
|
@ -32,11 +32,11 @@ public class CustomMediaTypeControllerTest {
|
||||
|
||||
@Test
|
||||
public void givenServiceUrl_whenGetWithProperAcceptHeaderFirstAPIVersion_thenReturn200() throws Exception {
|
||||
mockMvc.perform(get("/public/api/endpoint").accept("application/vnd.baeldung.api.v1+json")).andExpect(status().isOk());
|
||||
mockMvc.perform(get("/public/api/items/1").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());
|
||||
mockMvc.perform(get("/public/api/items/2").accept("application/vnd.baeldung.api.v2+json")).andExpect(status().isOk());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user