Skip to content

Commit fb6d5f2

Browse files
committed
Merge branch 'skoiloth-master'
2 parents 7335ef2 + e7ec500 commit fb6d5f2

9 files changed

Lines changed: 696 additions & 49 deletions

File tree

‎spring-rest-docs/pom.xml‎

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<properties>
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2323
<java.version>1.8</java.version>
24+
<snippetsDirectory>${project.build.directory}/generated-snippets</snippetsDirectory>
2425
</properties>
2526

2627
<dependencies>
@@ -44,16 +45,53 @@
4445
<version>1.0.1.RELEASE</version>
4546
<scope>test</scope>
4647
</dependency>
48+
<dependency>
49+
<groupId>com.jayway.jsonpath</groupId>
50+
<artifactId>json-path</artifactId>
51+
<version>2.0.0</version>
52+
</dependency>
4753
</dependencies>
4854

49-
<build>
50-
<plugins>
51-
<plugin>
52-
<groupId>org.springframework.boot</groupId>
53-
<artifactId>spring-boot-maven-plugin</artifactId>
54-
</plugin>
55-
</plugins>
56-
</build>
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-maven-plugin</artifactId>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-surefire-plugin</artifactId>
64+
<configuration>
65+
<includes>
66+
<include>**/*Documentation.java</include>
67+
</includes>
68+
</configuration>
69+
</plugin>
70+
<plugin>
71+
<groupId>org.asciidoctor</groupId>
72+
<artifactId>asciidoctor-maven-plugin</artifactId>
73+
<version>1.5.2</version>
74+
<executions>
75+
<execution>
76+
<id>generate-docs</id>
77+
<phase>package</phase>
78+
<goals>
79+
<goal>process-asciidoc</goal>
80+
</goals>
81+
<configuration>
82+
<backend>html</backend>
83+
<doctype>book</doctype>
84+
<attributes>
85+
<snippets>${snippetsDirectory}</snippets>
86+
</attributes>
87+
<sourceDirectory>src/docs/asciidocs</sourceDirectory>
88+
<outputDirectory>target/generated-docs</outputDirectory>
89+
</configuration>
90+
</execution>
91+
</executions>
92+
</plugin>
93+
</plugins>
94+
</build>
5795

5896

5997
</project>
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
= RESTful Notes API Guide
2+
Baeldung;
3+
:doctype: book
4+
:icons: font
5+
:source-highlighter: highlightjs
6+
:toc: left
7+
:toclevels: 4
8+
:sectlinks:
9+
10+
[[overview]]
11+
= Overview
12+
13+
[[overview-http-verbs]]
14+
== HTTP verbs
15+
16+
RESTful notes tries to adhere as closely as possible to standard HTTP and REST conventions in its
17+
use of HTTP verbs.
18+
19+
|===
20+
| Verb | Usage
21+
22+
| `GET`
23+
| Used to retrieve a resource
24+
25+
| `POST`
26+
| Used to create a new resource
27+
28+
| `PATCH`
29+
| Used to update an existing resource, including partial updates
30+
31+
| `DELETE`
32+
| Used to delete an existing resource
33+
|===
34+
35+
RESTful notes tries to adhere as closely as possible to standard HTTP and REST conventions in its
36+
use of HTTP status codes.
37+
38+
|===
39+
| Status code | Usage
40+
41+
| `200 OK`
42+
| The request completed successfully
43+
44+
| `201 Created`
45+
| A new resource has been created successfully. The resource's URI is available from the response's
46+
`Location` header
47+
48+
| `204 No Content`
49+
| An update to an existing resource has been applied successfully
50+
51+
| `400 Bad Request`
52+
| The request was malformed. The response body will include an error providing further information
53+
54+
| `404 Not Found`
55+
| The requested resource did not exist
56+
|===
57+
58+
[[overview-headers]]
59+
== Headers
60+
61+
Every response has the following header(s):
62+
63+
include::{snippets}/headers-example/response-headers.adoc[]
64+
65+
[[overview-hypermedia]]
66+
== Hypermedia
67+
68+
RESTful Notes uses hypermedia and resources include links to other resources in their
69+
responses. Responses are in http://stateless.co/hal_specification.html[Hypertext Application
70+
from resource to resource.
71+
Language (HAL)] format. Links can be found beneath the `_links` key. Users of the API should
72+
not create URIs themselves, instead they should use the above-described links to navigate
73+
74+
[[resources]]
75+
= Resources
76+
77+
78+
79+
[[resources-index]]
80+
== Index
81+
82+
The index provides the entry point into the service.
83+
84+
[[resources-index-access]]
85+
=== Accessing the index
86+
87+
A `GET` request is used to access the index
88+
89+
==== Response structure
90+
91+
include::{snippets}/index-example/http-response.adoc[]
92+
93+
==== Example response
94+
95+
include::{snippets}/index-example/http-response.adoc[]
96+
97+
==== Example request
98+
99+
include::{snippets}/index-example/http-request.adoc[]
100+
101+
==== CURL request
102+
103+
include::{snippets}/index-example/curl-request.adoc[]
104+
105+
[[resources-index-links]]
106+
==== Links
107+
108+
include::{snippets}/index-example/links.adoc[]
109+
110+
111+
[[resources-CRUD]]
112+
== CRUD REST Service
113+
114+
The CRUD provides the entry point into the service.
115+
116+
[[resources-crud-access]]
117+
=== Accessing the crud GET
118+
119+
A `GET` request is used to access the CRUD read
120+
121+
==== Response structure
122+
123+
include::{snippets}/crud-get-example/http-request.adoc[]
124+
125+
==== Example response
126+
127+
include::{snippets}/crud-get-example/http-response.adoc[]
128+
129+
==== CURL request
130+
131+
include::{snippets}/crud-get-example/curl-request.adoc[]
132+
133+
[[resources-crud-access]]
134+
=== Accessing the crud POST
135+
136+
A `POST` request is used to access the CRUD create
137+
138+
==== Response structure
139+
140+
include::{snippets}/crud-create-example/http-request.adoc[]
141+
142+
==== Example response
143+
144+
include::{snippets}/crud-create-example/http-response.adoc[]
145+
146+
==== CURL request
147+
148+
include::{snippets}/crud-create-example/curl-request.adoc[]
149+
150+
[[resources-crud-access]]
151+
=== Accessing the crud DELETE
152+
153+
A `DELETE` request is used to access the CRUD create
154+
155+
==== Response structure
156+
157+
include::{snippets}/crud-delete-example/http-request.adoc[]
158+
159+
==== Example response
160+
161+
include::{snippets}/crud-delete-example/http-response.adoc[]
162+
163+
==== CURL request
164+
165+
include::{snippets}/crud-delete-example/curl-request.adoc[]
166+
167+
[[resources-crud-access]]
168+
=== Accessing the crud PATCH
169+
170+
A `PATCH` request is used to access the CRUD create
171+
172+
==== Response structure
173+
174+
include::{snippets}/crud-patch-example/http-request.adoc[]
175+
176+
==== Example response
177+
178+
include::{snippets}/crud-patch-example/http-response.adoc[]
179+
180+
==== CURL request
181+
182+
include::{snippets}/crud-patch-example/curl-request.adoc[]
183+
184+
[[resources-crud-access]]
185+
=== Accessing the crud PUT
186+
187+
A `PUT` request is used to access the CRUD create
188+
189+
==== Response structure
190+
191+
include::{snippets}/crud-put-example/http-request.adoc[]
192+
193+
==== Example response
194+
195+
include::{snippets}/crud-put-example/http-response.adoc[]
196+
197+
==== CURL request
198+
199+
include::{snippets}/crud-put-example/curl-request.adoc[]
200+
201+
202+
203+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.example;
2+
3+
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.springframework.http.HttpHeaders;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.bind.annotation.RequestBody;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
import org.springframework.web.bind.annotation.ResponseStatus;
15+
import org.springframework.web.bind.annotation.RestController;
16+
17+
@RestController
18+
@RequestMapping("/crud")
19+
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+
}
55+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.example;
2+
3+
import java.net.URI;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
import org.hibernate.validator.constraints.NotBlank;
8+
9+
import com.fasterxml.jackson.annotation.JsonCreator;
10+
import com.fasterxml.jackson.annotation.JsonProperty;
11+
12+
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+
}
41+
42+
}

0 commit comments

Comments
 (0)