Clean up Spring REST Docs examples

This commit is contained in:
David Morley
2016-04-15 05:53:48 -05:00
parent 52728c6ef9
commit 46da6af6ba
11 changed files with 108 additions and 550 deletions

View File

@@ -1,23 +1,23 @@
package com.example;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
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;
}
@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;
}
}

View File

@@ -7,10 +7,10 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/rest/api")
public class MyRestController {
@RequestMapping(method=RequestMethod.GET)
public String index() {
return "Hello";
}
@RequestMapping(method = RequestMethod.GET)
public String index() {
return "Hello";
}
}

View File

@@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringRestDocsApplication {
public static void main(String[] args) {
SpringApplication.run(SpringRestDocsApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(SpringRestDocsApplication.class, args);
}
}

View File

@@ -1,12 +1,5 @@
package com.example;
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
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;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -21,43 +14,53 @@ 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.operation.preprocess.Preprocessors.prettyPrint;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
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 <<Simple descrition about the REST Service >>"),linkWithRel("tags").description("The <<resources-tags,Tags resource>>")),
responseFields(fieldWithPath("_links").description("<<resources-index-links,Links>> to other resources")));
this.mockMvc.perform(get("/api")).andExpect(status().isOk());
}
@Test
public void contextLoads() {
}
@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 <<Simple description about the REST Service >>"),
linkWithRel("tags").description("The <<resources-tags,Tags resource>>")
),
responseFields(fieldWithPath("_links").description("<<resources-index-links,Links>> to other resources")));
this.mockMvc.perform(get("/api")).andExpect(status().isOk());
}
@Test
public void contextLoads() {
}
}