コード例 #1
0
  @Test
  public void crudCreateExample() throws Exception {
    Map<String, String> tag = new HashMap<>();
    tag.put("name", "CREATE");

    String tagLocation =
        this.mockMvc
            .perform(
                post("/crud")
                    .contentType(MediaTypes.HAL_JSON)
                    .content(this.objectMapper.writeValueAsString(tag)))
            .andExpect(status().isCreated())
            .andReturn()
            .getResponse()
            .getHeader("Location");

    Map<String, Object> crud = new HashMap<>();
    crud.put("title", "Sample Model");
    crud.put("body", "http://www.baeldung.com/");
    crud.put("tags", singletonList(tagLocation));

    ConstrainedFields fields = new ConstrainedFields(CrudInput.class);
    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")));
    this.mockMvc
        .perform(
            post("/crud")
                .contentType(MediaTypes.HAL_JSON)
                .content(this.objectMapper.writeValueAsString(crud)))
        .andExpect(status().isCreated());
  }