注释中的Spring-restdocs字段描述

时间:2016-12-07 14:41:15

标签: spring-restdocs spring-auto-restdocs

是否可以使用注释(在字段级别)为字段提供说明?

我知道我可以使用description方法

.andDo(document("index", responseFields( 
            fieldWithPath("contact").description("The user's contact details"), 

但我更愿意将该描述与字段定义一起放在我的响应对象中。

class IndexResponse {
 //The user's contact details
 String contract;
}

我知道我可以生成约束描述(http://docs.spring.io/spring-restdocs/docs/current/reference/html5/#_using_constraint_descriptions_in_generated_snippets),但它只为验证注释生成了描述。

我正在寻找来自Swagger的https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodelproperty

2 个答案:

答案 0 :(得分:4)

我们构建了Spring REST Docs的扩展,允许使用Javadoc进行字段描述:

class IndexResponse {
  /**
   * The user's contact details
   */
  String contract;
}

但目前只有在使用Jackson和MockMvc测试时才有效。

项目:https://github.com/ScaCap/spring-auto-restdocs

简介文章:https://dzone.com/articles/introducing-spring-auto-rest-docs

答案 1 :(得分:2)

没有。我是REST Docs项目的负责人,我认为注释不是编写文档的好方法。如果您不同意该意见并想要使用注释,您可以编写一个类似于约束描述所做的附加组件。您可以将一个类传递给内省并自动生成FieldDescriptor个实例,然后您可以将这些实例传递到请求和响应字段片段。