Swagger @ApiModelProperty Usage Guide

The @ApiModelProperty annotation in Swagger is used to provide information about model attributes, such as the attribute name, data type, example value, default value, and whether it is required.

The specific usage is as follows:

  1. Use the @ApiModelProperty annotation on the properties of the model class to specify the description of the property.
  2. Specify the name of the attribute using the value property.
  3. Specify the data type of the property through the dataType attribute.
  4. Specify the example value for the attribute using the example property.
  5. The required attribute specifies whether a property is necessary, with a default value of false.
  6. Specify the default value for a property through the defaultValue attribute.

Here is a sample code:

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
}

In the above example, the @ApiModelProperty annotation is used to describe the id, username, and password attributes of the User class. The value property specifies the name of the attribute, the dataType property specifies the data type of the attribute, the example property specifies the example value of the attribute, and the required property specifies whether the attribute is required, with the default value being false.

This way, when generating Swagger documentation, information about model properties can be displayed based on the @ApiModelProperty annotation.

bannerAds