diff --git a/spring-rest-docs/pom.xml b/spring-rest-docs/pom.xml new file mode 100644 index 0000000000..decdd3a5df --- /dev/null +++ b/spring-rest-docs/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + com.example + demo + 0.0.1-SNAPSHOT + jar + + spring-rest-docs + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.3.3.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-hateoas + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.restdocs + spring-restdocs-mockmvc + 1.0.1.RELEASE + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-rest-docs/src/main/java/com/example/IndexController.java b/spring-rest-docs/src/main/java/com/example/IndexController.java new file mode 100644 index 0000000000..92d987f05d --- /dev/null +++ b/spring-rest-docs/src/main/java/com/example/IndexController.java @@ -0,0 +1,23 @@ +package com.example; + + +import org.springframework.hateoas.ResourceSupport; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; + +@RestController +@RequestMapping("/api") +public class IndexController { + + @RequestMapping(method = RequestMethod.GET) + public ResourceSupport index() { + ResourceSupport index = new ResourceSupport(); + index.add(linkTo(MyRestController.class).withRel("notes")); + index.add(linkTo(MyRestController.class).withRel("tags")); + return index; + } + +} \ No newline at end of file diff --git a/spring-rest-docs/src/main/java/com/example/MyRestController.java b/spring-rest-docs/src/main/java/com/example/MyRestController.java new file mode 100644 index 0000000000..896b82abfb --- /dev/null +++ b/spring-rest-docs/src/main/java/com/example/MyRestController.java @@ -0,0 +1,16 @@ +package com.example; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/rest/api") +public class MyRestController { + + @RequestMapping(method = RequestMethod.GET) + public String index() { + return "Hello"; + } + +} diff --git a/spring-rest-docs/src/main/java/com/example/SpringRestDocsApplication.java b/spring-rest-docs/src/main/java/com/example/SpringRestDocsApplication.java new file mode 100644 index 0000000000..da09f9accc --- /dev/null +++ b/spring-rest-docs/src/main/java/com/example/SpringRestDocsApplication.java @@ -0,0 +1,12 @@ +package com.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringRestDocsApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringRestDocsApplication.class, args); + } +} diff --git a/spring-rest-docs/src/main/resources/application.properties b/spring-rest-docs/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-rest-docs/src/test/java/com/example/ApiDocumentation.java b/spring-rest-docs/src/test/java/com/example/ApiDocumentation.java new file mode 100644 index 0000000000..5490e90ff5 --- /dev/null +++ b/spring-rest-docs/src/test/java/com/example/ApiDocumentation.java @@ -0,0 +1,66 @@ +package com.example; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.restdocs.RestDocumentation; +import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel; +import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; +import static org.springframework.restdocs.operation.preprocess.Preprocessors.*; +import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; +import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = SpringRestDocsApplication.class) +@WebAppConfiguration +public class ApiDocumentation { + + @Rule + public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets"); + + @Autowired + private WebApplicationContext context; + + private RestDocumentationResultHandler document; + + private MockMvc mockMvc; + + @Before + public void setUp() { + this.document = document("{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint())); + + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) + .apply(documentationConfiguration(this.restDocumentation)) + .alwaysDo(this.document).build(); + } + + @Test + public void indexExample() throws Exception { + this.document.snippets( + links( + linkWithRel("notes").description("The <>"), + linkWithRel("tags").description("The <>") + ), + responseFields(fieldWithPath("_links").description("<> to other resources"))); + + this.mockMvc.perform(get("/api")).andExpect(status().isOk()); + } + + @Test + public void contextLoads() { + } +} \ No newline at end of file