Spring boot integrates swagger gracefully

Keywords: Java Spring Spring Boot RESTful swagger

Import dependency

            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.9.2</version>
            </dependency>

            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.9.2</version>
            </dependency>

configuration file

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.djy.demo.controller"))//Scan package
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("Interface documentation").description("Here is the description").version("1.0").build();
    }
}

Code use

@Data
public class ApiUserVO {

    @ApiModelProperty(value = "Primary key")
    private String id;

    @ApiModelProperty(value = "password")
    private String username;

}
@RestController
@RequestMapping("/api")
@Api(tags = "test swagger Interface")
public class SaggerController {

    @ApiOperation(value = "This is the first swagger Interface")
    @PostMapping("/swagger")
    public SwaggerVO testSwagger(@RequestBody SwaggerVO vo) {
        return vo;
    }
}

visit http://localhost:8080/swagger-ui.html

Common notes

  • @Api() is used for class;
    Indicates that this class is a resource of swagger
  • @ApiOperation() is used for the method;
    Represents the operation of an http request
  • @ApiParam() is used for method, parameter and field description;
    Indicates the metadata added to the parameter (description or required, etc.)
  • @ApiModel() for class
    Represents a description of the class, which is used to receive parameters with entity classes
  • @ApiModelProperty() is used for methods and fields
    Represents a description of the model property or a data operation change
  • @ApiIgnore() is used for classes, methods, and method parameters
    Indicates that this method or class is ignored
  • @ApiImplicitParam() for method
    Represents a separate request parameter
  • @ApiImplicitParams() is used for the method and contains multiple @ apiimplicitparams

Specific use examples:

@Api()
For class; Indicates that this class is a resource of swagger
tags - indicates the description
value - is also a description. You can use tags instead
Description - right side description

@Api(value="user controller",tags={"User operation interface"})
@RestController
public class UserController { }

@ApiOperation()
For the method; Represents the operation of an http request
value - used for method description
notes - used to prompt content
tags can be regrouped (as appropriate)

@ApiParam()
Used for method, parameter and field description; Indicates the metadata added to the parameter (description or required, etc.)
Name - parameter name
value - parameter description
Required - required

@Api(value="user controller",tags={"User operation interface"})
@RestController
public class UserController {
    @ApiOperation(value="Get user information",tags={"Get user information copy"},notes="Attention points")
    @GetMapping("/getUserInfo")
    public User getUserInfo(@ApiParam(name="id",value="user id",required=true) Long id,
                            @ApiParam(name="username",value="user name") String username) {
        // userService can be ignored and is business logic 
        User user = userService.getUserInfo();
        return user;
    }
}

@ApiModel()
For class; Represents a description of the class, which is used to receive parameters with entity classes
value - represents the object name
Description - description can be omitted

@ApiModelProperty()
Used for methods, fields; Represents a description of the model property or a data operation change
value - field description
Name - override attribute name
dataType - override property type
Required - required
example - example
Hidden - hidden

    @ApiModel(value = "user object", description = "User object user")
    public class User implements Serializable {
        private static final long serialVersionUID = 1L;
        @ApiModelProperty(value = "user name", name = "username", example = "xingguo")
        private String username;
        @ApiModelProperty(value = "state", name = "state", required = true)
        private Integer state;
        private String password;
        private String nickName;
        private Integer isDeleted;
        @ApiModelProperty(value = "id array", hidden = true)
        private String[] ids;
        private List<String> idList;
        //Omit get/set
    }

@ApiIgnore()
For classes or methods, it can not be displayed on the page by swagger
It's relatively simple. There are no examples here

@ApiImplicitParam()
For method
Represents a separate request parameter

@ApiImplicitParams()
Used for method, including multiple @ apiimplicitparams
Name - parameter name
value - parameter description
dataType - data type
paramType - parameter type
example - example

    @ApiOperation("Query test")
    @GetMapping("select")
    @ApiImplicitParam(name = "name", value = "user name", dataType = "String", paramType = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "user name", dataType = "string", paramType = "query", example = "xingguo"), 
            @ApiImplicitParam(name = "id", value = "user id", dataType = "long", paramType = "query")})
    public void select() {
    }

Posted by Michael Lasky on Thu, 09 Sep 2021 15:06:36 -0700