温馨提示×

Swagger中apimodelproperty的用法是什么

小亿
107
2024-02-02 09:21:33
栏目: 编程语言

Swagger中的@ApiModelProperty注解用于描述模型属性的信息,包括属性名称、数据类型、示例值、默认值、是否必需等。

具体用法如下:

  1. 在模型类的属性上使用@ApiModelProperty注解,指定属性的描述信息。
  2. 通过value属性指定属性的名称。
  3. 通过dataType属性指定属性的数据类型。
  4. 通过example属性指定属性的示例值。
  5. 通过required属性指定属性是否必需,默认为false。
  6. 通过defaultValue属性指定属性的默认值。

示例代码如下:

public class User {
    @ApiModelProperty(value = "用户ID", dataType = "Long", example = "1")
    private Long id;
  
    @ApiModelProperty(value = "用户名", dataType = "String", required = true)
    private String username;
  
    @ApiModelProperty(value = "密码", dataType = "String")
    private String password;
  
    // getters and setters
}

在上面的示例中,@ApiModelProperty注解分别用于描述User类的id、username和password属性。通过value属性指定属性的名称,dataType属性指定属性的数据类型,example属性指定属性的示例值,required属性指定属性是否必需,默认为false。

这样,在生成Swagger文档时,就可以根据@ApiModelProperty注解的信息来展示模型属性的相关信息。

0