Skip to content

Commit c977b6f

Browse files
authored
Merge pull request eugenp#3370 from eugenp/update-rest-docs
update restdocs, move to spring-5
2 parents 92e8b0b + fa2e225 commit c977b6f

15 files changed

Lines changed: 514 additions & 537 deletions

File tree

‎pom.xml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@
203203
<module>spring-protobuf</module>
204204
<module>spring-quartz</module>
205205
<module>spring-rest-angular</module>
206-
<module>spring-rest-docs</module>
207206
<module>spring-rest-full</module>
208207
<module>spring-rest-query-language</module>
209208
<module>spring-rest</module>

‎spring-5/pom.xml‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
<groupId>org.springframework.boot</groupId>
4040
<artifactId>spring-boot-starter-webflux</artifactId>
4141
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-hateoas</artifactId>
45+
</dependency>
4246
<dependency>
4347
<groupId>org.projectreactor</groupId>
4448
<artifactId>reactor-spring</artifactId>
@@ -135,6 +139,23 @@
135139
<version>${junit.platform.version}</version>
136140
<scope>test</scope>
137141
</dependency>
142+
<!-- restdocs -->
143+
<dependency>
144+
<groupId>org.springframework.restdocs</groupId>
145+
<artifactId>spring-restdocs-mockmvc</artifactId>
146+
<scope>test</scope>
147+
</dependency>
148+
<dependency>
149+
<groupId>org.springframework.restdocs</groupId>
150+
<artifactId>spring-restdocs-webtestclient</artifactId>
151+
<scope>test</scope>
152+
</dependency>
153+
<dependency>
154+
<groupId>org.springframework.restdocs</groupId>
155+
<artifactId>spring-restdocs-restassured</artifactId>
156+
<scope>test</scope>
157+
</dependency>
158+
138159

139160
</dependencies>
140161

@@ -163,6 +184,29 @@
163184
</excludes>
164185
</configuration>
165186
</plugin>
187+
<plugin>
188+
<groupId>org.asciidoctor</groupId>
189+
<artifactId>asciidoctor-maven-plugin</artifactId>
190+
<version>${asciidoctor-plugin.version}</version>
191+
<executions>
192+
<execution>
193+
<id>generate-docs</id>
194+
<phase>package</phase>
195+
<goals>
196+
<goal>process-asciidoc</goal>
197+
</goals>
198+
<configuration>
199+
<backend>html</backend>
200+
<doctype>book</doctype>
201+
<attributes>
202+
<snippets>${snippetsDirectory}</snippets>
203+
</attributes>
204+
<sourceDirectory>src/docs/asciidocs</sourceDirectory>
205+
<outputDirectory>target/generated-docs</outputDirectory>
206+
</configuration>
207+
</execution>
208+
</executions>
209+
</plugin>
166210
</plugins>
167211
</build>
168212

@@ -199,6 +243,8 @@
199243
<johnzon.version>1.1.3</johnzon.version>
200244
<jsonb-api.version>1.0</jsonb-api.version>
201245
<geronimo-json_1.1_spec.version>1.0</geronimo-json_1.1_spec.version>
246+
<asciidoctor-plugin.version>1.5.6</asciidoctor-plugin.version>
247+
<snippetsDirectory>${project.build.directory}/generated-snippets</snippetsDirectory>
202248
</properties>
203249

204250
</project>

spring-rest-docs/src/docs/asciidocs/api-guide.adoc renamed to spring-5/src/docs/asciidocs/api-guide.adoc

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,6 @@ use of HTTP status codes.
5555
| The requested resource did not exist
5656
|===
5757

58-
[[overview-headers]]
59-
== Headers
60-
61-
Every response has the following header(s):
62-
63-
include::{snippets}/headers-example/response-headers.adoc[]
64-
6558
[[overview-hypermedia]]
6659
== Hypermedia
6760

@@ -86,18 +79,14 @@ The index provides the entry point into the service.
8679

8780
A `GET` request is used to access the index
8881

89-
==== Response structure
82+
==== Request structure
9083

91-
include::{snippets}/index-example/http-response.adoc[]
84+
include::{snippets}/index-example/http-request.adoc[]
9285

9386
==== Example response
9487

9588
include::{snippets}/index-example/http-response.adoc[]
9689

97-
==== Example request
98-
99-
include::{snippets}/index-example/http-request.adoc[]
100-
10190
==== CURL request
10291

10392
include::{snippets}/index-example/curl-request.adoc[]
@@ -113,12 +102,12 @@ include::{snippets}/index-example/links.adoc[]
113102

114103
The CRUD provides the entry point into the service.
115104

116-
[[resources-crud-access]]
105+
[[resources-crud-get]]
117106
=== Accessing the crud GET
118107

119-
A `GET` request is used to access the CRUD read
108+
A `GET` request is used to access the CRUD read.
120109

121-
==== Response structure
110+
==== Request structure
122111

123112
include::{snippets}/crud-get-example/http-request.adoc[]
124113

@@ -130,12 +119,12 @@ include::{snippets}/crud-get-example/http-response.adoc[]
130119

131120
include::{snippets}/crud-get-example/curl-request.adoc[]
132121

133-
[[resources-crud-access]]
122+
[[resources-crud-post]]
134123
=== Accessing the crud POST
135124

136-
A `POST` request is used to access the CRUD create
125+
A `POST` request is used to access the CRUD create.
137126

138-
==== Response structure
127+
==== Request structure
139128

140129
include::{snippets}/crud-create-example/http-request.adoc[]
141130

@@ -147,15 +136,18 @@ include::{snippets}/crud-create-example/http-response.adoc[]
147136

148137
include::{snippets}/crud-create-example/curl-request.adoc[]
149138

150-
[[resources-crud-access]]
139+
[[resources-crud-delete]]
151140
=== Accessing the crud DELETE
152141

153-
A `DELETE` request is used to access the CRUD create
142+
A `DELETE` request is used to access the CRUD delete.
154143

155-
==== Response structure
144+
==== Request structure
156145

157146
include::{snippets}/crud-delete-example/http-request.adoc[]
158147

148+
==== Path Parameters
149+
include::{snippets}/crud-delete-example/path-parameters.adoc[]
150+
159151
==== Example response
160152

161153
include::{snippets}/crud-delete-example/http-response.adoc[]
@@ -164,12 +156,12 @@ include::{snippets}/crud-delete-example/http-response.adoc[]
164156

165157
include::{snippets}/crud-delete-example/curl-request.adoc[]
166158

167-
[[resources-crud-access]]
159+
[[resources-crud-patch]]
168160
=== Accessing the crud PATCH
169161

170-
A `PATCH` request is used to access the CRUD create
162+
A `PATCH` request is used to access the CRUD update.
171163

172-
==== Response structure
164+
==== Request structure
173165

174166
include::{snippets}/crud-patch-example/http-request.adoc[]
175167

@@ -181,12 +173,12 @@ include::{snippets}/crud-patch-example/http-response.adoc[]
181173

182174
include::{snippets}/crud-patch-example/curl-request.adoc[]
183175

184-
[[resources-crud-access]]
176+
[[resources-crud-put]]
185177
=== Accessing the crud PUT
186178

187-
A `PUT` request is used to access the CRUD create
179+
A `PUT` request is used to access the CRUD update.
188180

189-
==== Response structure
181+
==== Request structure
190182

191183
include::{snippets}/crud-put-example/http-request.adoc[]
192184

spring-rest-docs/src/main/java/com/example/CRUDController.java renamed to spring-5/src/main/java/com/baeldung/restdocs/CRUDController.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,62 @@
1-
package com.example;
1+
package com.baeldung.restdocs;
22

33
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
44

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;
13+
import org.springframework.web.bind.annotation.DeleteMapping;
14+
import org.springframework.web.bind.annotation.GetMapping;
15+
import org.springframework.web.bind.annotation.PatchMapping;
1016
import org.springframework.web.bind.annotation.PathVariable;
17+
import org.springframework.web.bind.annotation.PostMapping;
18+
import org.springframework.web.bind.annotation.PutMapping;
1119
import org.springframework.web.bind.annotation.RequestBody;
1220
import org.springframework.web.bind.annotation.RequestMapping;
13-
import org.springframework.web.bind.annotation.RequestMethod;
1421
import org.springframework.web.bind.annotation.ResponseStatus;
1522
import org.springframework.web.bind.annotation.RestController;
1623

1724
@RestController
1825
@RequestMapping("/crud")
1926
public class CRUDController {
2027

21-
@RequestMapping(method = RequestMethod.GET)
22-
@ResponseStatus(HttpStatus.OK)
23-
public List<CrudInput> read(@RequestBody CrudInput crudInput) {
24-
List<CrudInput> returnList = new ArrayList<CrudInput>();
28+
@GetMapping
29+
public List<CrudInput> read(@RequestBody @Valid CrudInput crudInput) {
30+
List<CrudInput> returnList = new ArrayList<>();
2531
returnList.add(crudInput);
2632
return returnList;
2733
}
2834

2935
@ResponseStatus(HttpStatus.CREATED)
30-
@RequestMapping(method = RequestMethod.POST)
31-
public HttpHeaders save(@RequestBody CrudInput crudInput) {
36+
@PostMapping
37+
public HttpHeaders save(@RequestBody @Valid CrudInput crudInput) {
3238
HttpHeaders httpHeaders = new HttpHeaders();
33-
httpHeaders.setLocation(linkTo(CRUDController.class).slash(crudInput.getTitle()).toUri());
39+
httpHeaders.setLocation(linkTo(CRUDController.class).slash(crudInput.getId()).toUri());
3440
return httpHeaders;
3541
}
3642

37-
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
43+
@DeleteMapping("/{id}")
3844
@ResponseStatus(HttpStatus.OK)
39-
HttpHeaders delete(@RequestBody CrudInput crudInput) {
40-
HttpHeaders httpHeaders = new HttpHeaders();
41-
return httpHeaders;
45+
HttpHeaders delete(@PathVariable("id") long id) {
46+
return new HttpHeaders();
4247
}
4348

44-
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
49+
@PutMapping("/{id}")
4550
@ResponseStatus(HttpStatus.ACCEPTED)
4651
void put(@PathVariable("id") long id, @RequestBody CrudInput crudInput) {
4752

4853
}
4954

50-
@RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
51-
@ResponseStatus(HttpStatus.NO_CONTENT)
52-
void patch(@PathVariable("id") long id, @RequestBody CrudInput crudInput) {
53-
55+
@PatchMapping("/{id}")
56+
public List<CrudInput> patch(@PathVariable("id") long id, @RequestBody CrudInput crudInput) {
57+
List<CrudInput> returnList = new ArrayList<CrudInput>();
58+
crudInput.setId(id);
59+
returnList.add(crudInput);
60+
return returnList;
5461
}
5562
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.restdocs;
2+
3+
import java.net.URI;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
import javax.validation.constraints.NotBlank;
8+
import javax.validation.constraints.NotNull;
9+
10+
import com.fasterxml.jackson.annotation.JsonCreator;
11+
import com.fasterxml.jackson.annotation.JsonProperty;
12+
13+
public class CrudInput {
14+
15+
@NotNull
16+
private long id;
17+
18+
@NotBlank
19+
private String title;
20+
21+
private String body;
22+
23+
private List<URI> tagUris;
24+
25+
@JsonCreator
26+
public CrudInput(@JsonProperty("id") long id, @JsonProperty("title") String title, @JsonProperty("body") String body, @JsonProperty("tags") List<URI> tagUris) {
27+
this.id=id;
28+
this.title = title;
29+
this.body = body;
30+
this.tagUris = tagUris == null ? Collections.<URI> emptyList() : tagUris;
31+
}
32+
33+
public String getTitle() {
34+
return title;
35+
}
36+
37+
public String getBody() {
38+
return body;
39+
}
40+
41+
public long getId() {
42+
return id;
43+
}
44+
45+
public void setId(long id) {
46+
this.id = id;
47+
}
48+
49+
public void setTitle(String title) {
50+
this.title = title;
51+
}
52+
53+
public void setBody(String body) {
54+
this.body = body;
55+
}
56+
57+
public void setTagUris(List<URI> tagUris) {
58+
this.tagUris = tagUris;
59+
}
60+
61+
@JsonProperty("tags")
62+
public List<URI> getTagUris() {
63+
return this.tagUris;
64+
}
65+
66+
}

spring-rest-docs/src/main/java/com/example/IndexController.java renamed to spring-5/src/main/java/com/baeldung/restdocs/IndexController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package com.example;
1+
package com.baeldung.restdocs;
22

33
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
44

55
import org.springframework.hateoas.ResourceSupport;
6+
import org.springframework.web.bind.annotation.GetMapping;
67
import org.springframework.web.bind.annotation.RequestMapping;
7-
import org.springframework.web.bind.annotation.RequestMethod;
88
import org.springframework.web.bind.annotation.RestController;
99

1010
@RestController
1111
@RequestMapping("/")
1212
public class IndexController {
1313

14-
@RequestMapping(method = RequestMethod.GET)
14+
@GetMapping
1515
public ResourceSupport index() {
1616
ResourceSupport index = new ResourceSupport();
1717
index.add(linkTo(CRUDController.class).withRel("crud"));

spring-rest-docs/src/main/java/com/example/SpringRestDocsApplication.java renamed to spring-5/src/main/java/com/baeldung/restdocs/SpringRestDocsApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example;
1+
package com.baeldung.restdocs;
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;

0 commit comments

Comments
 (0)