Skip to content

Commit 152d9b3

Browse files
author
eugenp
committed
minor formatting work
1 parent bf78a04 commit 152d9b3

6 files changed

Lines changed: 179 additions & 251 deletions

File tree

‎spring-rest-docs/src/main/java/com/example/CRUDController.java‎

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,39 @@
1717
@RestController
1818
@RequestMapping("/crud")
1919
public class CRUDController {
20-
21-
@RequestMapping(method=RequestMethod.GET)
22-
@ResponseStatus(HttpStatus.OK)
23-
public List<CrudInput> read(@RequestBody CrudInput crudInput) {
24-
List<CrudInput> returnList=new ArrayList<CrudInput>();
25-
returnList.add(crudInput);
26-
return returnList;
27-
}
28-
29-
@ResponseStatus(HttpStatus.CREATED)
30-
@RequestMapping(method=RequestMethod.POST)
31-
public HttpHeaders save(@RequestBody CrudInput crudInput) {
32-
HttpHeaders httpHeaders = new HttpHeaders();
33-
httpHeaders.setLocation(linkTo(CRUDController.class).slash(crudInput.getTitle()).toUri());
34-
return httpHeaders;
35-
}
36-
37-
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
38-
@ResponseStatus(HttpStatus.OK)
39-
HttpHeaders delete(@RequestBody CrudInput crudInput) {
40-
HttpHeaders httpHeaders = new HttpHeaders();
41-
return httpHeaders;
42-
}
43-
44-
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
45-
@ResponseStatus(HttpStatus.ACCEPTED)
46-
void put(@PathVariable("id") long id, @RequestBody CrudInput crudInput) {
47-
48-
}
49-
50-
@RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
51-
@ResponseStatus(HttpStatus.NO_CONTENT)
52-
void patch(@PathVariable("id") long id, @RequestBody CrudInput crudInput) {
53-
54-
}
20+
21+
@RequestMapping(method = RequestMethod.GET)
22+
@ResponseStatus(HttpStatus.OK)
23+
public List<CrudInput> read(@RequestBody CrudInput crudInput) {
24+
List<CrudInput> returnList = new ArrayList<CrudInput>();
25+
returnList.add(crudInput);
26+
return returnList;
27+
}
28+
29+
@ResponseStatus(HttpStatus.CREATED)
30+
@RequestMapping(method = RequestMethod.POST)
31+
public HttpHeaders save(@RequestBody CrudInput crudInput) {
32+
HttpHeaders httpHeaders = new HttpHeaders();
33+
httpHeaders.setLocation(linkTo(CRUDController.class).slash(crudInput.getTitle()).toUri());
34+
return httpHeaders;
35+
}
36+
37+
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
38+
@ResponseStatus(HttpStatus.OK)
39+
HttpHeaders delete(@RequestBody CrudInput crudInput) {
40+
HttpHeaders httpHeaders = new HttpHeaders();
41+
return httpHeaders;
42+
}
43+
44+
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
45+
@ResponseStatus(HttpStatus.ACCEPTED)
46+
void put(@PathVariable("id") long id, @RequestBody CrudInput crudInput) {
47+
48+
}
49+
50+
@RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
51+
@ResponseStatus(HttpStatus.NO_CONTENT)
52+
void patch(@PathVariable("id") long id, @RequestBody CrudInput crudInput) {
53+
54+
}
5555
}

‎spring-rest-docs/src/main/java/com/example/CrudInput.java‎

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,32 @@
1010
import com.fasterxml.jackson.annotation.JsonProperty;
1111

1212
public class CrudInput {
13-
14-
//@NotBlank
15-
private final String title;
16-
17-
private final String body;
18-
19-
private final List<URI> tagUris;
20-
21-
@JsonCreator
22-
public CrudInput(@JsonProperty("title") String title,
23-
@JsonProperty("body") String body, @JsonProperty("tags") List<URI> tagUris) {
24-
this.title = title;
25-
this.body = body;
26-
this.tagUris = tagUris == null ? Collections.<URI>emptyList() : tagUris;
27-
}
28-
29-
public String getTitle() {
30-
return title;
31-
}
32-
33-
public String getBody() {
34-
return body;
35-
}
36-
37-
@JsonProperty("tags")
38-
public List<URI> getTagUris() {
39-
return this.tagUris;
40-
}
13+
14+
// @NotBlank
15+
private final String title;
16+
17+
private final String body;
18+
19+
private final List<URI> tagUris;
20+
21+
@JsonCreator
22+
public CrudInput(@JsonProperty("title") String title, @JsonProperty("body") String body, @JsonProperty("tags") List<URI> tagUris) {
23+
this.title = title;
24+
this.body = body;
25+
this.tagUris = tagUris == null ? Collections.<URI> emptyList() : tagUris;
26+
}
27+
28+
public String getTitle() {
29+
return title;
30+
}
31+
32+
public String getBody() {
33+
return body;
34+
}
35+
36+
@JsonProperty("tags")
37+
public List<URI> getTagUris() {
38+
return this.tagUris;
39+
}
4140

4241
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.example;
22

3-
43
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
54

65
import org.springframework.hateoas.ResourceSupport;
@@ -12,11 +11,11 @@
1211
@RequestMapping("/")
1312
public class IndexController {
1413

15-
@RequestMapping(method=RequestMethod.GET)
16-
public ResourceSupport index() {
17-
ResourceSupport index = new ResourceSupport();
18-
index.add(linkTo(CRUDController.class).withRel("crud"));
19-
return index;
20-
}
14+
@RequestMapping(method = RequestMethod.GET)
15+
public ResourceSupport index() {
16+
ResourceSupport index = new ResourceSupport();
17+
index.add(linkTo(CRUDController.class).withRel("crud"));
18+
return index;
19+
}
2120

2221
}

‎spring-rest-docs/src/main/java/com/example/SpringRestDocsApplication.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@SpringBootApplication
77
public class SpringRestDocsApplication {
88

9-
public static void main(String[] args) {
10-
SpringApplication.run(SpringRestDocsApplication.class, args);
11-
}
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringRestDocsApplication.class, args);
11+
}
1212
}

‎spring-rest-docs/src/test/java/com/example/ApiDocumentation.java‎

Lines changed: 16 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -56,83 +56,53 @@ public class ApiDocumentation {
5656
@Before
5757
public void setUp() {
5858
this.document = document("{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()));
59-
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
60-
.apply(documentationConfiguration(this.restDocumentation))
61-
.alwaysDo(this.document)
62-
.build();
59+
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).apply(documentationConfiguration(this.restDocumentation)).alwaysDo(this.document).build();
6360
}
6461

65-
6662
@Test
6763
public void headersExample() throws Exception {
68-
this.document.snippets(responseHeaders(headerWithName("Content-Type")
69-
.description("The Content-Type of the payload, e.g. `application/hal+json`")));
70-
this.mockMvc.perform(get("/"))
71-
.andExpect(status().isOk());
64+
this.document.snippets(responseHeaders(headerWithName("Content-Type").description("The Content-Type of the payload, e.g. `application/hal+json`")));
65+
this.mockMvc.perform(get("/")).andExpect(status().isOk());
7266
}
7367

7468
@Test
7569
public void indexExample() throws Exception {
76-
this.document.snippets(
77-
links(linkWithRel("crud").description("The <<resources-tags,Tags resource>>")),
78-
responseFields(fieldWithPath("_links").description("<<resources-index-links,Links>> to other resources"))
79-
);
80-
this.mockMvc.perform(get("/"))
81-
.andExpect(status().isOk());
70+
this.document.snippets(links(linkWithRel("crud").description("The <<resources-tags,Tags resource>>")), responseFields(fieldWithPath("_links").description("<<resources-index-links,Links>> to other resources")));
71+
this.mockMvc.perform(get("/")).andExpect(status().isOk());
8272
}
8373

84-
8574
@Test
8675
public void crudGetExample() throws Exception {
8776

8877
Map<String, String> tag = new HashMap<>();
8978
tag.put("name", "GET");
9079

91-
String tagLocation = this.mockMvc.perform(get("/crud")
92-
.contentType(MediaTypes.HAL_JSON)
93-
.content(this.objectMapper.writeValueAsString(tag)))
94-
.andExpect(status().isOk())
95-
.andReturn()
96-
.getResponse()
97-
.getHeader("Location");
80+
String tagLocation = this.mockMvc.perform(get("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isOk()).andReturn().getResponse().getHeader("Location");
9881

9982
Map<String, Object> crud = new HashMap<>();
10083
crud.put("title", "Sample Model");
10184
crud.put("body", "http://www.baeldung.com/");
10285
crud.put("tags", singletonList(tagLocation));
10386

104-
this.mockMvc.perform(get("/crud")
105-
.contentType(MediaTypes.HAL_JSON)
106-
.content(this.objectMapper.writeValueAsString(crud)))
107-
.andExpect(status().isOk());
87+
this.mockMvc.perform(get("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isOk());
10888
}
10989

11090
@Test
11191
public void crudCreateExample() throws Exception {
11292
Map<String, String> tag = new HashMap<>();
11393
tag.put("name", "CREATE");
11494

115-
String tagLocation = this.mockMvc.perform(post("/crud")
116-
.contentType(MediaTypes.HAL_JSON)
117-
.content(this.objectMapper.writeValueAsString(tag)))
118-
.andExpect(status().isCreated())
119-
.andReturn()
120-
.getResponse()
121-
.getHeader("Location");
95+
String tagLocation = this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isCreated()).andReturn().getResponse().getHeader("Location");
12296

12397
Map<String, Object> crud = new HashMap<>();
12498
crud.put("title", "Sample Model");
12599
crud.put("body", "http://www.baeldung.com/");
126100
crud.put("tags", singletonList(tagLocation));
127101

128102
ConstrainedFields fields = new ConstrainedFields(CrudInput.class);
129-
this.document.snippets(requestFields(
130-
fields.withPath("title").description("The title of the note"),
131-
fields.withPath("body").description("The body of the note"),
132-
fields.withPath("tags").description("An array of tag resource URIs")));
103+
this.document.snippets(requestFields(fields.withPath("title").description("The title of the note"), fields.withPath("body").description("The body of the note"), fields.withPath("tags").description("An array of tag resource URIs")));
133104
this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isCreated());
134105

135-
136106
}
137107

138108
@Test
@@ -141,23 +111,14 @@ public void crudDeleteExample() throws Exception {
141111
Map<String, String> tag = new HashMap<>();
142112
tag.put("name", "DELETE");
143113

144-
String tagLocation = this.mockMvc.perform(delete("/crud/10")
145-
.contentType(MediaTypes.HAL_JSON)
146-
.content(this.objectMapper.writeValueAsString(tag)))
147-
.andExpect(status().isOk())
148-
.andReturn()
149-
.getResponse()
150-
.getHeader("Location");
114+
String tagLocation = this.mockMvc.perform(delete("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isOk()).andReturn().getResponse().getHeader("Location");
151115

152116
Map<String, Object> crud = new HashMap<>();
153117
crud.put("title", "Sample Model");
154118
crud.put("body", "http://www.baeldung.com/");
155119
crud.put("tags", singletonList(tagLocation));
156120

157-
this.mockMvc.perform(delete("/crud/10")
158-
.contentType(MediaTypes.HAL_JSON)
159-
.content(this.objectMapper.writeValueAsString(crud)))
160-
.andExpect(status().isOk());
121+
this.mockMvc.perform(delete("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isOk());
161122
}
162123

163124
@Test
@@ -166,51 +127,31 @@ public void crudPatchExample() throws Exception {
166127
Map<String, String> tag = new HashMap<>();
167128
tag.put("name", "PATCH");
168129

169-
String tagLocation = this.mockMvc.perform(patch("/crud/10")
170-
.contentType(MediaTypes.HAL_JSON)
171-
.content(this.objectMapper.writeValueAsString(tag)))
172-
.andExpect(status().isNoContent())
173-
.andReturn()
174-
.getResponse()
175-
.getHeader("Location");
130+
String tagLocation = this.mockMvc.perform(patch("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isNoContent()).andReturn().getResponse().getHeader("Location");
176131

177132
Map<String, Object> crud = new HashMap<>();
178133
crud.put("title", "Sample Model");
179134
crud.put("body", "http://www.baeldung.com/");
180135
crud.put("tags", singletonList(tagLocation));
181136

182-
this.mockMvc.perform(patch("/crud/10")
183-
.contentType(MediaTypes.HAL_JSON)
184-
.content(this.objectMapper.writeValueAsString(crud)))
185-
.andExpect(status().isNoContent());
137+
this.mockMvc.perform(patch("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isNoContent());
186138
}
187139

188-
189140
@Test
190141
public void crudPutExample() throws Exception {
191142
Map<String, String> tag = new HashMap<>();
192143
tag.put("name", "PUT");
193144

194-
String tagLocation = this.mockMvc.perform(put("/crud/10")
195-
.contentType(MediaTypes.HAL_JSON)
196-
.content(this.objectMapper.writeValueAsString(tag)))
197-
.andExpect(status().isAccepted())
198-
.andReturn()
199-
.getResponse()
200-
.getHeader("Location");
145+
String tagLocation = this.mockMvc.perform(put("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isAccepted()).andReturn().getResponse().getHeader("Location");
201146

202147
Map<String, Object> crud = new HashMap<>();
203148
crud.put("title", "Sample Model");
204149
crud.put("body", "http://www.baeldung.com/");
205150
crud.put("tags", singletonList(tagLocation));
206151

207-
this.mockMvc.perform(put("/crud/10")
208-
.contentType(MediaTypes.HAL_JSON)
209-
.content(this.objectMapper.writeValueAsString(crud)))
210-
.andExpect(status().isAccepted());
152+
this.mockMvc.perform(put("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isAccepted());
211153
}
212154

213-
214155
@Test
215156
public void contextLoads() {
216157
}
@@ -224,11 +165,8 @@ private static class ConstrainedFields {
224165
}
225166

226167
private FieldDescriptor withPath(String path) {
227-
return fieldWithPath(path)
228-
.attributes(key("constraints")
229-
.value(collectionToDelimitedString(this.constraintDescriptions.descriptionsForProperty(path), ". ")));
168+
return fieldWithPath(path).attributes(key("constraints").value(collectionToDelimitedString(this.constraintDescriptions.descriptionsForProperty(path), ". ")));
230169
}
231170
}
232171

233-
234172
}

0 commit comments

Comments
 (0)