大摇大摆地使用@ApiParam 或@ApiModelProperty?
Use @ApiParam or @ApiModelProperty in swagger?
以下两个注释都适用于将元数据添加到 swagger-ui 文档。应该首选哪一个,为什么?
public class MyReq {
@ApiModelProperty(required = true, value = "the persons name")
@ApiParam(required = true, value = "the persons name")
private String name;
}
@RestController
public class MyServlet {
@RequestMapping("/")
public void test(MyReq req) {
}
}
两者有很大的区别。它们都用于向 swagger 添加元数据,但它们添加的元数据不同。
@ApiParam
为参数。通常定义在API资源请求class.
@ApiParam 示例:
/users?age=50
可用于定义参数age及以下字段:
- 参数类型:查询
- 姓名:年龄
- 描述:用户年龄
- 必填:真
@ApiModelProperty
用于为模型添加属性。
您将在模型属性 class 中使用它。
示例:
model User 具有姓名和年龄属性:姓名和年龄然后对于每个 属性 您可以定义以下内容:
适合年龄:
- 类型:整数,
- 格式": int64,
- 描述:用户年龄,
查看每个在 swagger 对象中表示的字段:
@ApiModelProperty-https://github.com/OAI/OpenAPI-Specification/blob/master/versions/1.2.md#529-property-object
@ApiParam - https://github.com/OAI/OpenAPI-Specification/blob/master/versions/1.2.md#524-parameter-object
以下两个注释都适用于将元数据添加到 swagger-ui 文档。应该首选哪一个,为什么?
public class MyReq {
@ApiModelProperty(required = true, value = "the persons name")
@ApiParam(required = true, value = "the persons name")
private String name;
}
@RestController
public class MyServlet {
@RequestMapping("/")
public void test(MyReq req) {
}
}
两者有很大的区别。它们都用于向 swagger 添加元数据,但它们添加的元数据不同。
@ApiParam
为参数。通常定义在API资源请求class.
@ApiParam 示例:
/users?age=50
可用于定义参数age及以下字段:
- 参数类型:查询
- 姓名:年龄
- 描述:用户年龄
- 必填:真
@ApiModelProperty
用于为模型添加属性。
您将在模型属性 class 中使用它。
示例:
model User 具有姓名和年龄属性:姓名和年龄然后对于每个 属性 您可以定义以下内容:
适合年龄:
- 类型:整数,
- 格式": int64,
- 描述:用户年龄,
查看每个在 swagger 对象中表示的字段:
@ApiModelProperty-https://github.com/OAI/OpenAPI-Specification/blob/master/versions/1.2.md#529-property-object
@ApiParam - https://github.com/OAI/OpenAPI-Specification/blob/master/versions/1.2.md#524-parameter-object