Skip to content

Commit cbf878b

Browse files
tomekl007Eugen
authored andcommitted
Custom Media Types for REST (eugenp#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
1 parent 082a45e commit cbf878b

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung.web.controller.mediatypes;
2+
3+
4+
import org.baeldung.web.dto.BaeldungItem;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.ResponseBody;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping(value = "/", produces = "application/vnd.baeldung.api.v1+json",
13+
consumes = "application/vnd.baeldung.api.v1+json")
14+
public class CustomMediaTypeController {
15+
16+
@RequestMapping(value = "/public/api/endpoint", produces = "application/vnd.baeldung.api.v1+json",
17+
consumes = "application/vnd.baeldung.api.v1+json")
18+
public @ResponseBody ResponseEntity<BaeldungItem> getItem() {
19+
return new ResponseEntity<>(new BaeldungItem("itemId1"), HttpStatus.OK);
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.web.dto;
2+
3+
4+
public class BaeldungItem {
5+
private final String itemId;
6+
7+
public BaeldungItem(String itemId) {
8+
this.itemId = itemId;
9+
}
10+
11+
public String getItemId() {
12+
return itemId;
13+
}
14+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.baeldung.web.controller.mediatypes;
2+
3+
import org.baeldung.config.WebConfig;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
import org.springframework.test.context.web.WebAppConfiguration;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
13+
import org.springframework.web.context.WebApplicationContext;
14+
15+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
16+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
17+
18+
19+
@RunWith(SpringJUnit4ClassRunner.class)
20+
@ContextConfiguration(classes = WebConfig.class)
21+
@WebAppConfiguration
22+
public class CustomMediaTypeControllerTest {
23+
24+
private MockMvc mockMvc;
25+
26+
@Autowired
27+
private WebApplicationContext webApplicationContext;
28+
29+
@Before
30+
public void setUp() {
31+
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
32+
}
33+
34+
@Test
35+
public void shouldSendRequestForItem() throws Exception {
36+
mockMvc.perform(get("/public/api/endpoint")
37+
.contentType("application/vnd.baeldung.api.v1+json")
38+
.accept("application/vnd.baeldung.api.v1+json"))
39+
.andExpect(status().isOk());
40+
}
41+
}

0 commit comments

Comments
 (0)