Skip to content

Commit 0815d31

Browse files
committed
Add examples for Spring REST Docs article
2 parents 4ab9fe6 + 46da6af commit 0815d31

6 files changed

Lines changed: 176 additions & 0 deletions

File tree

‎spring-rest-docs/pom.xml‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.example</groupId>
7+
<artifactId>demo</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-rest-docs</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.3.3.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-hateoas</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-web</artifactId>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-test</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.restdocs</groupId>
43+
<artifactId>spring-restdocs-mockmvc</artifactId>
44+
<version>1.0.1.RELEASE</version>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
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>
57+
58+
59+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example;
2+
3+
4+
import org.springframework.hateoas.ResourceSupport;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
10+
11+
@RestController
12+
@RequestMapping("/api")
13+
public class IndexController {
14+
15+
@RequestMapping(method = RequestMethod.GET)
16+
public ResourceSupport index() {
17+
ResourceSupport index = new ResourceSupport();
18+
index.add(linkTo(MyRestController.class).withRel("notes"));
19+
index.add(linkTo(MyRestController.class).withRel("tags"));
20+
return index;
21+
}
22+
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RequestMethod;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
@RestController
8+
@RequestMapping("/rest/api")
9+
public class MyRestController {
10+
11+
@RequestMapping(method = RequestMethod.GET)
12+
public String index() {
13+
return "Hello";
14+
}
15+
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.example;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringRestDocsApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringRestDocsApplication.class, args);
11+
}
12+
}

‎spring-rest-docs/src/main/resources/application.properties‎

Whitespace-only changes.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.example;
2+
3+
import org.junit.Before;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.SpringApplicationConfiguration;
9+
import org.springframework.restdocs.RestDocumentation;
10+
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
11+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12+
import org.springframework.test.context.web.WebAppConfiguration;
13+
import org.springframework.test.web.servlet.MockMvc;
14+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
15+
import org.springframework.web.context.WebApplicationContext;
16+
17+
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
18+
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
19+
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
20+
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
21+
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
22+
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;
23+
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
24+
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
25+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
26+
27+
@RunWith(SpringJUnit4ClassRunner.class)
28+
@SpringApplicationConfiguration(classes = SpringRestDocsApplication.class)
29+
@WebAppConfiguration
30+
public class ApiDocumentation {
31+
32+
@Rule
33+
public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");
34+
35+
@Autowired
36+
private WebApplicationContext context;
37+
38+
private RestDocumentationResultHandler document;
39+
40+
private MockMvc mockMvc;
41+
42+
@Before
43+
public void setUp() {
44+
this.document = document("{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()));
45+
46+
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
47+
.apply(documentationConfiguration(this.restDocumentation))
48+
.alwaysDo(this.document).build();
49+
}
50+
51+
@Test
52+
public void indexExample() throws Exception {
53+
this.document.snippets(
54+
links(
55+
linkWithRel("notes").description("The <<Simple description about the REST Service >>"),
56+
linkWithRel("tags").description("The <<resources-tags,Tags resource>>")
57+
),
58+
responseFields(fieldWithPath("_links").description("<<resources-index-links,Links>> to other resources")));
59+
60+
this.mockMvc.perform(get("/api")).andExpect(status().isOk());
61+
}
62+
63+
@Test
64+
public void contextLoads() {
65+
}
66+
}

0 commit comments

Comments
 (0)