minor formatting work

This commit is contained in:
eugenp 2016-07-26 11:34:56 +03:00
parent bf78a04b68
commit 152d9b38de
6 changed files with 173 additions and 245 deletions

View File

@ -18,38 +18,38 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/crud") @RequestMapping("/crud")
public class CRUDController { public class CRUDController {
@RequestMapping(method=RequestMethod.GET) @RequestMapping(method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public List<CrudInput> read(@RequestBody CrudInput crudInput) { public List<CrudInput> read(@RequestBody CrudInput crudInput) {
List<CrudInput> returnList=new ArrayList<CrudInput>(); List<CrudInput> returnList = new ArrayList<CrudInput>();
returnList.add(crudInput); returnList.add(crudInput);
return returnList; return returnList;
} }
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
@RequestMapping(method=RequestMethod.POST) @RequestMapping(method = RequestMethod.POST)
public HttpHeaders save(@RequestBody CrudInput crudInput) { public HttpHeaders save(@RequestBody CrudInput crudInput) {
HttpHeaders httpHeaders = new HttpHeaders(); HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setLocation(linkTo(CRUDController.class).slash(crudInput.getTitle()).toUri()); httpHeaders.setLocation(linkTo(CRUDController.class).slash(crudInput.getTitle()).toUri());
return httpHeaders; return httpHeaders;
} }
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE) @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
HttpHeaders delete(@RequestBody CrudInput crudInput) { HttpHeaders delete(@RequestBody CrudInput crudInput) {
HttpHeaders httpHeaders = new HttpHeaders(); HttpHeaders httpHeaders = new HttpHeaders();
return httpHeaders; return httpHeaders;
} }
@RequestMapping(value = "/{id}", method = RequestMethod.PUT) @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.ACCEPTED) @ResponseStatus(HttpStatus.ACCEPTED)
void put(@PathVariable("id") long id, @RequestBody CrudInput crudInput) { void put(@PathVariable("id") long id, @RequestBody CrudInput crudInput) {
} }
@RequestMapping(value = "/{id}", method = RequestMethod.PATCH) @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
@ResponseStatus(HttpStatus.NO_CONTENT) @ResponseStatus(HttpStatus.NO_CONTENT)
void patch(@PathVariable("id") long id, @RequestBody CrudInput crudInput) { void patch(@PathVariable("id") long id, @RequestBody CrudInput crudInput) {
} }
} }

View File

@ -11,32 +11,31 @@ import com.fasterxml.jackson.annotation.JsonProperty;
public class CrudInput { public class CrudInput {
//@NotBlank // @NotBlank
private final String title; private final String title;
private final String body; private final String body;
private final List<URI> tagUris; private final List<URI> tagUris;
@JsonCreator @JsonCreator
public CrudInput(@JsonProperty("title") String title, public CrudInput(@JsonProperty("title") String title, @JsonProperty("body") String body, @JsonProperty("tags") List<URI> tagUris) {
@JsonProperty("body") String body, @JsonProperty("tags") List<URI> tagUris) { this.title = title;
this.title = title; this.body = body;
this.body = body; this.tagUris = tagUris == null ? Collections.<URI> emptyList() : tagUris;
this.tagUris = tagUris == null ? Collections.<URI>emptyList() : tagUris; }
}
public String getTitle() { public String getTitle() {
return title; return title;
} }
public String getBody() { public String getBody() {
return body; return body;
} }
@JsonProperty("tags") @JsonProperty("tags")
public List<URI> getTagUris() { public List<URI> getTagUris() {
return this.tagUris; return this.tagUris;
} }
} }

View File

@ -1,6 +1,5 @@
package com.example; package com.example;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import org.springframework.hateoas.ResourceSupport; import org.springframework.hateoas.ResourceSupport;
@ -12,11 +11,11 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/") @RequestMapping("/")
public class IndexController { public class IndexController {
@RequestMapping(method=RequestMethod.GET) @RequestMapping(method = RequestMethod.GET)
public ResourceSupport index() { public ResourceSupport index() {
ResourceSupport index = new ResourceSupport(); ResourceSupport index = new ResourceSupport();
index.add(linkTo(CRUDController.class).withRel("crud")); index.add(linkTo(CRUDController.class).withRel("crud"));
return index; return index;
} }
} }

View File

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

View File

@ -56,55 +56,35 @@ public class ApiDocumentation {
@Before @Before
public void setUp() { public void setUp() {
this.document = document("{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint())); this.document = document("{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()));
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).apply(documentationConfiguration(this.restDocumentation)).alwaysDo(this.document).build();
.apply(documentationConfiguration(this.restDocumentation))
.alwaysDo(this.document)
.build();
} }
@Test @Test
public void headersExample() throws Exception { public void headersExample() throws Exception {
this.document.snippets(responseHeaders(headerWithName("Content-Type") this.document.snippets(responseHeaders(headerWithName("Content-Type").description("The Content-Type of the payload, e.g. `application/hal+json`")));
.description("The Content-Type of the payload, e.g. `application/hal+json`"))); this.mockMvc.perform(get("/")).andExpect(status().isOk());
this.mockMvc.perform(get("/"))
.andExpect(status().isOk());
} }
@Test @Test
public void indexExample() throws Exception { public void indexExample() throws Exception {
this.document.snippets( this.document.snippets(links(linkWithRel("crud").description("The <<resources-tags,Tags resource>>")), responseFields(fieldWithPath("_links").description("<<resources-index-links,Links>> to other resources")));
links(linkWithRel("crud").description("The <<resources-tags,Tags resource>>")), this.mockMvc.perform(get("/")).andExpect(status().isOk());
responseFields(fieldWithPath("_links").description("<<resources-index-links,Links>> to other resources"))
);
this.mockMvc.perform(get("/"))
.andExpect(status().isOk());
} }
@Test @Test
public void crudGetExample() throws Exception { public void crudGetExample() throws Exception {
Map<String, String> tag = new HashMap<>(); Map<String, String> tag = new HashMap<>();
tag.put("name", "GET"); tag.put("name", "GET");
String tagLocation = this.mockMvc.perform(get("/crud") String tagLocation = this.mockMvc.perform(get("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isOk()).andReturn().getResponse().getHeader("Location");
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(tag)))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getHeader("Location");
Map<String, Object> crud = new HashMap<>(); Map<String, Object> crud = new HashMap<>();
crud.put("title", "Sample Model"); crud.put("title", "Sample Model");
crud.put("body", "http://www.baeldung.com/"); crud.put("body", "http://www.baeldung.com/");
crud.put("tags", singletonList(tagLocation)); crud.put("tags", singletonList(tagLocation));
this.mockMvc.perform(get("/crud") this.mockMvc.perform(get("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isOk());
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(crud)))
.andExpect(status().isOk());
} }
@Test @Test
@ -112,13 +92,7 @@ public class ApiDocumentation {
Map<String, String> tag = new HashMap<>(); Map<String, String> tag = new HashMap<>();
tag.put("name", "CREATE"); tag.put("name", "CREATE");
String tagLocation = this.mockMvc.perform(post("/crud") String tagLocation = this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isCreated()).andReturn().getResponse().getHeader("Location");
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(tag)))
.andExpect(status().isCreated())
.andReturn()
.getResponse()
.getHeader("Location");
Map<String, Object> crud = new HashMap<>(); Map<String, Object> crud = new HashMap<>();
crud.put("title", "Sample Model"); crud.put("title", "Sample Model");
@ -126,13 +100,9 @@ public class ApiDocumentation {
crud.put("tags", singletonList(tagLocation)); crud.put("tags", singletonList(tagLocation));
ConstrainedFields fields = new ConstrainedFields(CrudInput.class); ConstrainedFields fields = new ConstrainedFields(CrudInput.class);
this.document.snippets(requestFields( this.document.snippets(requestFields(fields.withPath("title").description("The title of the note"), fields.withPath("body").description("The body of the note"), fields.withPath("tags").description("An array of tag resource URIs")));
fields.withPath("title").description("The title of the note"),
fields.withPath("body").description("The body of the note"),
fields.withPath("tags").description("An array of tag resource URIs")));
this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isCreated()); this.mockMvc.perform(post("/crud").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isCreated());
} }
@Test @Test
@ -141,23 +111,14 @@ public class ApiDocumentation {
Map<String, String> tag = new HashMap<>(); Map<String, String> tag = new HashMap<>();
tag.put("name", "DELETE"); tag.put("name", "DELETE");
String tagLocation = this.mockMvc.perform(delete("/crud/10") String tagLocation = this.mockMvc.perform(delete("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isOk()).andReturn().getResponse().getHeader("Location");
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(tag)))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getHeader("Location");
Map<String, Object> crud = new HashMap<>(); Map<String, Object> crud = new HashMap<>();
crud.put("title", "Sample Model"); crud.put("title", "Sample Model");
crud.put("body", "http://www.baeldung.com/"); crud.put("body", "http://www.baeldung.com/");
crud.put("tags", singletonList(tagLocation)); crud.put("tags", singletonList(tagLocation));
this.mockMvc.perform(delete("/crud/10") this.mockMvc.perform(delete("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isOk());
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(crud)))
.andExpect(status().isOk());
} }
@Test @Test
@ -166,51 +127,31 @@ public class ApiDocumentation {
Map<String, String> tag = new HashMap<>(); Map<String, String> tag = new HashMap<>();
tag.put("name", "PATCH"); tag.put("name", "PATCH");
String tagLocation = this.mockMvc.perform(patch("/crud/10") String tagLocation = this.mockMvc.perform(patch("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isNoContent()).andReturn().getResponse().getHeader("Location");
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(tag)))
.andExpect(status().isNoContent())
.andReturn()
.getResponse()
.getHeader("Location");
Map<String, Object> crud = new HashMap<>(); Map<String, Object> crud = new HashMap<>();
crud.put("title", "Sample Model"); crud.put("title", "Sample Model");
crud.put("body", "http://www.baeldung.com/"); crud.put("body", "http://www.baeldung.com/");
crud.put("tags", singletonList(tagLocation)); crud.put("tags", singletonList(tagLocation));
this.mockMvc.perform(patch("/crud/10") this.mockMvc.perform(patch("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isNoContent());
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(crud)))
.andExpect(status().isNoContent());
} }
@Test @Test
public void crudPutExample() throws Exception { public void crudPutExample() throws Exception {
Map<String, String> tag = new HashMap<>(); Map<String, String> tag = new HashMap<>();
tag.put("name", "PUT"); tag.put("name", "PUT");
String tagLocation = this.mockMvc.perform(put("/crud/10") String tagLocation = this.mockMvc.perform(put("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(tag))).andExpect(status().isAccepted()).andReturn().getResponse().getHeader("Location");
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(tag)))
.andExpect(status().isAccepted())
.andReturn()
.getResponse()
.getHeader("Location");
Map<String, Object> crud = new HashMap<>(); Map<String, Object> crud = new HashMap<>();
crud.put("title", "Sample Model"); crud.put("title", "Sample Model");
crud.put("body", "http://www.baeldung.com/"); crud.put("body", "http://www.baeldung.com/");
crud.put("tags", singletonList(tagLocation)); crud.put("tags", singletonList(tagLocation));
this.mockMvc.perform(put("/crud/10") this.mockMvc.perform(put("/crud/10").contentType(MediaTypes.HAL_JSON).content(this.objectMapper.writeValueAsString(crud))).andExpect(status().isAccepted());
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(crud)))
.andExpect(status().isAccepted());
} }
@Test @Test
public void contextLoads() { public void contextLoads() {
} }
@ -224,11 +165,8 @@ public class ApiDocumentation {
} }
private FieldDescriptor withPath(String path) { private FieldDescriptor withPath(String path) {
return fieldWithPath(path) return fieldWithPath(path).attributes(key("constraints").value(collectionToDelimitedString(this.constraintDescriptions.descriptionsForProperty(path), ". ")));
.attributes(key("constraints")
.value(collectionToDelimitedString(this.constraintDescriptions.descriptionsForProperty(path), ". ")));
} }
} }
} }

View File

@ -46,113 +46,105 @@ public class GettingStartedDocumentation {
private MockMvc mockMvc; private MockMvc mockMvc;
@Before @Before
public void setUp() { public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).apply(documentationConfiguration(this.restDocumentation)).alwaysDo(document("{method-name}/{step}/", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()))).build();
.apply(documentationConfiguration(this.restDocumentation))
.alwaysDo(document("{method-name}/{step}/",
preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint())))
.build();
} }
@Test @Test
public void index() throws Exception { public void index() throws Exception {
this.mockMvc.perform(get("/").accept(MediaTypes.HAL_JSON)) this.mockMvc.perform(get("/").accept(MediaTypes.HAL_JSON)).andExpect(status().isOk()).andExpect(jsonPath("_links.crud", is(notNullValue()))).andExpect(jsonPath("_links.crud", is(notNullValue())));
.andExpect(status().isOk())
.andExpect(jsonPath("_links.crud", is(notNullValue())))
.andExpect(jsonPath("_links.crud", is(notNullValue())));
} }
// String createNote() throws Exception { // String createNote() throws Exception {
// Map<String, String> note = new HashMap<String, String>(); // Map<String, String> note = new HashMap<String, String>();
// note.put("title", "Note creation with cURL"); // note.put("title", "Note creation with cURL");
// note.put("body", "An example of how to create a note using curl"); // note.put("body", "An example of how to create a note using curl");
// String noteLocation = this.mockMvc.perform(post("/crud") // String noteLocation = this.mockMvc.perform(post("/crud")
// .contentType(MediaTypes.HAL_JSON) // .contentType(MediaTypes.HAL_JSON)
// .content(objectMapper.writeValueAsString(note))) // .content(objectMapper.writeValueAsString(note)))
// .andExpect(status().isCreated()) // .andExpect(status().isCreated())
// .andExpect(header().string("Location", notNullValue())) // .andExpect(header().string("Location", notNullValue()))
// .andReturn() // .andReturn()
// .getResponse() // .getResponse()
// .getHeader("Location"); // .getHeader("Location");
// return noteLocation; // return noteLocation;
// } // }
// //
// MvcResult getNote(String noteLocation) throws Exception { // MvcResult getNote(String noteLocation) throws Exception {
// return this.mockMvc.perform(get(noteLocation)) // return this.mockMvc.perform(get(noteLocation))
// .andExpect(status().isOk()) // .andExpect(status().isOk())
// .andExpect(jsonPath("title", is(notNullValue()))) // .andExpect(jsonPath("title", is(notNullValue())))
// .andExpect(jsonPath("body", is(notNullValue()))) // .andExpect(jsonPath("body", is(notNullValue())))
// .andExpect(jsonPath("_links.crud", is(notNullValue()))) // .andExpect(jsonPath("_links.crud", is(notNullValue())))
// .andReturn(); // .andReturn();
// } // }
// //
// //
// String createTag() throws Exception, JsonProcessingException { // String createTag() throws Exception, JsonProcessingException {
// Map<String, String> tag = new HashMap<String, String>(); // Map<String, String> tag = new HashMap<String, String>();
// tag.put("name", "getting-started"); // tag.put("name", "getting-started");
// String tagLocation = this.mockMvc.perform(post("/crud") // String tagLocation = this.mockMvc.perform(post("/crud")
// .contentType(MediaTypes.HAL_JSON) // .contentType(MediaTypes.HAL_JSON)
// .content(objectMapper.writeValueAsString(tag))) // .content(objectMapper.writeValueAsString(tag)))
// .andExpect(status().isCreated()) // .andExpect(status().isCreated())
// .andExpect(header().string("Location", notNullValue())) // .andExpect(header().string("Location", notNullValue()))
// .andReturn() // .andReturn()
// .getResponse() // .getResponse()
// .getHeader("Location"); // .getHeader("Location");
// return tagLocation; // return tagLocation;
// } // }
// //
// void getTag(String tagLocation) throws Exception { // void getTag(String tagLocation) throws Exception {
// this.mockMvc.perform(get(tagLocation)).andExpect(status().isOk()) // this.mockMvc.perform(get(tagLocation)).andExpect(status().isOk())
// .andExpect(jsonPath("name", is(notNullValue()))) // .andExpect(jsonPath("name", is(notNullValue())))
// .andExpect(jsonPath("_links.tagged-notes", is(notNullValue()))); // .andExpect(jsonPath("_links.tagged-notes", is(notNullValue())));
// } // }
// //
// String createTaggedNote(String tag) throws Exception { // String createTaggedNote(String tag) throws Exception {
// Map<String, Object> note = new HashMap<String, Object>(); // Map<String, Object> note = new HashMap<String, Object>();
// note.put("title", "Tagged note creation with cURL"); // note.put("title", "Tagged note creation with cURL");
// note.put("body", "An example of how to create a tagged note using cURL"); // note.put("body", "An example of how to create a tagged note using cURL");
// note.put("tags", Arrays.asList(tag)); // note.put("tags", Arrays.asList(tag));
// //
// String noteLocation = this.mockMvc.perform(post("/notes") // String noteLocation = this.mockMvc.perform(post("/notes")
// .contentType(MediaTypes.HAL_JSON) // .contentType(MediaTypes.HAL_JSON)
// .content(objectMapper.writeValueAsString(note))) // .content(objectMapper.writeValueAsString(note)))
// .andExpect(status().isCreated()) // .andExpect(status().isCreated())
// .andExpect(header().string("Location", notNullValue())) // .andExpect(header().string("Location", notNullValue()))
// .andReturn() // .andReturn()
// .getResponse() // .getResponse()
// .getHeader("Location"); // .getHeader("Location");
// return noteLocation; // return noteLocation;
// } // }
// //
// void getTags(String noteTagsLocation) throws Exception { // void getTags(String noteTagsLocation) throws Exception {
// this.mockMvc.perform(get(noteTagsLocation)) // this.mockMvc.perform(get(noteTagsLocation))
// .andExpect(status().isOk()) // .andExpect(status().isOk())
// .andExpect(jsonPath("_embedded.tags", hasSize(1))); // .andExpect(jsonPath("_embedded.tags", hasSize(1)));
// } // }
// //
// void tagExistingNote(String noteLocation, String tagLocation) throws Exception { // void tagExistingNote(String noteLocation, String tagLocation) throws Exception {
// Map<String, Object> update = new HashMap<String, Object>(); // Map<String, Object> update = new HashMap<String, Object>();
// update.put("tags", Arrays.asList(tagLocation)); // update.put("tags", Arrays.asList(tagLocation));
// this.mockMvc.perform(patch(noteLocation) // this.mockMvc.perform(patch(noteLocation)
// .contentType(MediaTypes.HAL_JSON) // .contentType(MediaTypes.HAL_JSON)
// .content(objectMapper.writeValueAsString(update))) // .content(objectMapper.writeValueAsString(update)))
// .andExpect(status().isNoContent()); // .andExpect(status().isNoContent());
// } // }
// //
// MvcResult getTaggedExistingNote(String noteLocation) throws Exception { // MvcResult getTaggedExistingNote(String noteLocation) throws Exception {
// return this.mockMvc.perform(get(noteLocation)).andExpect(status().isOk()).andReturn(); // return this.mockMvc.perform(get(noteLocation)).andExpect(status().isOk()).andReturn();
// } // }
// //
// void getTagsForExistingNote(String noteTagsLocation) throws Exception { // void getTagsForExistingNote(String noteTagsLocation) throws Exception {
// this.mockMvc.perform(get(noteTagsLocation)) // this.mockMvc.perform(get(noteTagsLocation))
// .andExpect(status().isOk()).andExpect(jsonPath("_embedded.tags", hasSize(1))); // .andExpect(status().isOk()).andExpect(jsonPath("_embedded.tags", hasSize(1)));
// } // }
// //
// private String getLink(MvcResult result, String rel) // private String getLink(MvcResult result, String rel)
// throws UnsupportedEncodingException { // throws UnsupportedEncodingException {
// return JsonPath.parse(result.getResponse().getContentAsString()).read("_links." + rel + ".href"); // return JsonPath.parse(result.getResponse().getContentAsString()).read("_links." + rel + ".href");
// } // }
} }