Custom Media Types for REST (#918)

* 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
This commit is contained in:
Tomasz Lelek
2016-12-28 15:33:04 +01:00
committed by Eugen
parent 082a45ebd6
commit cbf878ba34
3 changed files with 76 additions and 0 deletions

View File

@@ -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<BaeldungItem> getItem() {
return new ResponseEntity<>(new BaeldungItem("itemId1"), HttpStatus.OK);
}
}

View File

@@ -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;
}
}