Skip to content

Commit fa2e225

Browse files
committed
add requested changes
1 parent 3d233f5 commit fa2e225

3 files changed

Lines changed: 30 additions & 39 deletions

File tree

‎spring-5/src/main/java/com/baeldung/restdocs/CRUDController.java‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
import java.util.ArrayList;
66
import java.util.List;
77

8+
import javax.validation.Valid;
9+
810
import org.springframework.http.HttpHeaders;
911
import org.springframework.http.HttpStatus;
12+
import org.springframework.validation.BindingResult;
1013
import org.springframework.web.bind.annotation.DeleteMapping;
1114
import org.springframework.web.bind.annotation.GetMapping;
1215
import org.springframework.web.bind.annotation.PatchMapping;
@@ -23,33 +26,33 @@
2326
public class CRUDController {
2427

2528
@GetMapping
26-
public List<CrudInput> read(@RequestBody CrudInput crudInput) {
27-
List<CrudInput> returnList = new ArrayList<CrudInput>();
29+
public List<CrudInput> read(@RequestBody @Valid CrudInput crudInput) {
30+
List<CrudInput> returnList = new ArrayList<>();
2831
returnList.add(crudInput);
2932
return returnList;
3033
}
3134

3235
@ResponseStatus(HttpStatus.CREATED)
3336
@PostMapping
34-
public HttpHeaders save(@RequestBody CrudInput crudInput) {
37+
public HttpHeaders save(@RequestBody @Valid CrudInput crudInput) {
3538
HttpHeaders httpHeaders = new HttpHeaders();
3639
httpHeaders.setLocation(linkTo(CRUDController.class).slash(crudInput.getId()).toUri());
3740
return httpHeaders;
3841
}
3942

40-
@DeleteMapping(value = "/{id}")
43+
@DeleteMapping("/{id}")
4144
@ResponseStatus(HttpStatus.OK)
4245
HttpHeaders delete(@PathVariable("id") long id) {
4346
return new HttpHeaders();
4447
}
4548

46-
@PutMapping(value = "/{id}")
49+
@PutMapping("/{id}")
4750
@ResponseStatus(HttpStatus.ACCEPTED)
4851
void put(@PathVariable("id") long id, @RequestBody CrudInput crudInput) {
4952

5053
}
5154

52-
@PatchMapping(value = "/{id}")
55+
@PatchMapping("/{id}")
5356
public List<CrudInput> patch(@PathVariable("id") long id, @RequestBody CrudInput crudInput) {
5457
List<CrudInput> returnList = new ArrayList<CrudInput>();
5558
crudInput.setId(id);

‎spring-5/src/test/java/com/baeldung/restdocs/ApiDocumentationJUnit4IntegrationTest.java‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,18 @@ public void indexExample() throws Exception {
7575
@Test
7676
public void crudGetExample() throws Exception {
7777

78-
Map<String, String> tag = new HashMap<>();
79-
tag.put("name", "GET");
78+
Map<String, Object> crud = new HashMap<>();
79+
crud.put("id", 1L);
80+
crud.put("title", "Sample Model");
81+
crud.put("body", "http://www.baeldung.com/");
8082

8183
String tagLocation = this.mockMvc.perform(get("/crud").contentType(MediaTypes.HAL_JSON)
82-
.content(this.objectMapper.writeValueAsString(tag)))
84+
.content(this.objectMapper.writeValueAsString(crud)))
8385
.andExpect(status().isOk())
8486
.andReturn()
8587
.getResponse()
8688
.getHeader("Location");
8789

88-
Map<String, Object> crud = new HashMap<>();
89-
crud.put("id", 1L);
90-
crud.put("title", "Sample Model");
91-
crud.put("body", "http://www.baeldung.com/");
9290
crud.put("tags", singletonList(tagLocation));
9391

9492
ConstraintDescriptions desc = new ConstraintDescriptions(CrudInput.class);
@@ -102,20 +100,18 @@ public void crudGetExample() throws Exception {
102100

103101
@Test
104102
public void crudCreateExample() throws Exception {
105-
Map<String, String> tag = new HashMap<>();
106-
tag.put("name", "CREATE");
103+
Map<String, Object> crud = new HashMap<>();
104+
crud.put("id", 2L);
105+
crud.put("title", "Sample Model");
106+
crud.put("body", "http://www.baeldung.com/");
107107

108108
String tagLocation = this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON)
109-
.content(this.objectMapper.writeValueAsString(tag)))
109+
.content(this.objectMapper.writeValueAsString(crud)))
110110
.andExpect(status().isCreated())
111111
.andReturn()
112112
.getResponse()
113113
.getHeader("Location");
114114

115-
Map<String, Object> crud = new HashMap<>();
116-
crud.put("id", 2L);
117-
crud.put("title", "Sample Model");
118-
crud.put("body", "http://www.baeldung.com/");
119115
crud.put("tags", singletonList(tagLocation));
120116

121117
this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON)

‎spring-5/src/test/java/com/baeldung/restdocs/ApiDocumentationJUnit5IntegrationTest.java‎

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
88
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
99
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
10-
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
11-
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
12-
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch;
13-
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
14-
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.put;
10+
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
1511
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
1612
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
1713
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
@@ -72,20 +68,18 @@ public void indexExample() throws Exception {
7268
@Test
7369
public void crudGetExample() throws Exception {
7470

75-
Map<String, String> tag = new HashMap<>();
76-
tag.put("name", "GET");
71+
Map<String, Object> crud = new HashMap<>();
72+
crud.put("id", 1L);
73+
crud.put("title", "Sample Model");
74+
crud.put("body", "http://www.baeldung.com/");
7775

7876
String tagLocation = this.mockMvc.perform(get("/crud").contentType(MediaTypes.HAL_JSON)
79-
.content(this.objectMapper.writeValueAsString(tag)))
77+
.content(this.objectMapper.writeValueAsString(crud)))
8078
.andExpect(status().isOk())
8179
.andReturn()
8280
.getResponse()
8381
.getHeader("Location");
8482

85-
Map<String, Object> crud = new HashMap<>();
86-
crud.put("id", 1L);
87-
crud.put("title", "Sample Model");
88-
crud.put("body", "http://www.baeldung.com/");
8983
crud.put("tags", singletonList(tagLocation));
9084

9185
ConstraintDescriptions desc = new ConstraintDescriptions(CrudInput.class);
@@ -99,20 +93,18 @@ public void crudGetExample() throws Exception {
9993

10094
@Test
10195
public void crudCreateExample() throws Exception {
102-
Map<String, String> tag = new HashMap<>();
103-
tag.put("name", "CREATE");
96+
Map<String, Object> crud = new HashMap<>();
97+
crud.put("id", 2L);
98+
crud.put("title", "Sample Model");
99+
crud.put("body", "http://www.baeldung.com/");
104100

105101
String tagLocation = this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON)
106-
.content(this.objectMapper.writeValueAsString(tag)))
102+
.content(this.objectMapper.writeValueAsString(crud)))
107103
.andExpect(status().isCreated())
108104
.andReturn()
109105
.getResponse()
110106
.getHeader("Location");
111107

112-
Map<String, Object> crud = new HashMap<>();
113-
crud.put("id", 2L);
114-
crud.put("title", "Sample Model");
115-
crud.put("body", "http://www.baeldung.com/");
116108
crud.put("tags", singletonList(tagLocation));
117109

118110
this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON)

0 commit comments

Comments
 (0)