我如何使用Spring Docs记录java.util.Map

mib*_*iti 1 spring spring-mvc spring-boot spring-restdocs

想象一下我下一堂课

public class MyDTO implements Serializable {
    private static final long serialVersionUID = 1L;

    private String id;
    private Map<String, String> names;

    // public Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)

当我使用下一个代码通过Spring对其进行记录时

private static FieldDescriptor[] myDTOFields() {
  return new FieldDescriptor[] { 
    fieldWithPath("id").description("id description"),
    fieldWithPath("names").description("Names description") };
}
Run Code Online (Sandbox Code Playgroud)

它不起作用,并且出现错误。

org.springframework.restdocs.snippet.SnippetException:有效负载的以下部分未记录:

{
  "names" : {
    "en" : "test"
  }
}
Run Code Online (Sandbox Code Playgroud)

那我怎么用spring docs记录java.util.Map?

谢谢 :)

And*_*son 6

文档中所述,您可以使用PayloadDocumentation.subsectionWithPath(“names”)它。这将意味着REST Docs会将names其包含的所有内容视为已被记录在案。