Java 大摇大摆地使用@ApiParam 或@ApiModelProperty?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44523777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:34:14  来源:igfitidea点击:

Use @ApiParam or @ApiModelProperty in swagger?

javaswaggerswagger-uiswagger-2.0

提问by membersound

Both the following annotations work for adding metadata to swagger-ui docs. Which one should be prefered, and why?

以下两个注释都适用于向 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) {

   }
}

采纳答案by slal

There is a huge difference between the two. They are both used to add metadata to swagger but they add different metadata.

两者之间存在巨大差异。它们都用于向 swagger 添加元数据,但它们添加了不同的元数据。

@ApiParamis for parameters. It is usually defined in the API Resource request class.

@ApiParam用于参数。它通常在 API Resource 请求类中定义。

Example of @ApiParam:

@ApiParam 示例:

/users?age=50

it can be used to define parameter age and the following fields:

它可用于定义参数年龄和以下字段:

  • paramType: query
  • name: age
  • description: age of the user
  • required: true
  • 参数类型:查询
  • 姓名年龄
  • 描述:用户年龄
  • 要求:真实

@ApiModelPropertyis used for adding properties for models. You will use it in your model class on the model properties.

@ApiModelProperty用于为模型添加属性。您将在模型属性的模型类中使用它。

Example:

例子:

model User has name and age as properties: name and age then for each property you can define the following:

模型用户具有名称和年龄作为属性:名称和年龄然后您可以为每个属性定义以下内容:

For age:

年龄:

  • type: integer,
  • format": int64,
  • description: age of the user,
  • 类型:整数,
  • 格式”:int64,
  • 描述:用户的年龄,

Check out the fields each denote in the swagger objects:

查看每个在 swagger 对象中表示的字段:

@ApiModelProperty- https://github.com/OAI/OpenAPI-Specification/blob/master/versions/1.2.md#529-property-object

@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

@ApiParam - https://github.com/OAI/OpenAPI-Specification/blob/master/versions/1.2.md#524-parameter-object