SpringBoot-集成Swagger3

For projects where the front and back ends are separated, the existence of interface documentation is very important. Different from manually writing interface documents, swagger is a tool that automatically generates interface documents. In an environment where requirements are constantly changing, the efficiency of manually writing documents is too low. Compared with the new version of swagger3, swagger2 has fewer configurations and is more convenient to use.

The first step is to add dependencies

<!--Swagger3-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>

It's a practical starter, but my maven can't download the swagger-ui, so I added a dependency

The second step is to configure Swagger3Config

@Configuration
public class Swagger3Config {
    
    
    @Bean
    public Docket createRestApi() {
    
    
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * contact类的路径是springfox.documentation.service.Contact;
     * @return
     */
    private ApiInfo apiInfo() {
    
    
        return new ApiInfoBuilder()
                .title("Swagger3接口文档")
                .description("更多请咨询服务开发者Ray。")
                .contact(new Contact("Ray。", "http://www.ruiyeclub.cn", "[email protected]"))
                .version("1.0")
                .build();
    }
}

The fourth step is to add @EnableOpenApi annotation

@SpringBootApplication
@EnableOpenApi
@MapperScan("com.swagger.demo.mapper")
public class SpringBootSwaggerTestApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(SpringBootSwaggerTestApplication.class, args);
    }

}

The fifth step is to write Html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>hello world!</h1>
<a href="/swagger-ui/index.html">查看Api</a>
</body>
</html>

The sixth step is to write Controller

@Controller
@Api(tags = "欢迎页面")
public class HelloController {
    
    
    @Autowired(required = false)
    UserService userService;

    @ApiOperation(value = "设置主页面",httpMethod="POST",notes="测试")
    @RequestMapping("/")
    public String index(){
    
    
        return "hello";
    }

    @ApiOperation(value = "获取所有用户",httpMethod="POST",notes="测试")
    @RequestMapping("/getAllUser")
    @ResponseBody
    public List<User> dataOfUser(){
    
    
        return userService.findAll();
    }
}

Step 7 start

Insert picture description here
Behind Url plus /swagger-ui/index.html
Insert picture description here
done
and is commonly used in explanatory notes

@Api: Used on the requested class, it means the description of the class
tags="The annotation that can be seen on the UI interface to explain the function of the class"
value="This parameter is meaningless, and it is also seen on the UI interface. So there is no need to configure"

@ApiOperation: Used on the requested method to explain the purpose and function of the
method value="Describe the purpose and function of the
method " notes="Remarks on the method"

@ApiImplicitParams: Used in the request method to indicate a set of parameter descriptions
@ApiImplicitParam: Used in the @ApiImplicitParams annotation to specify all aspects of a request parameter
name: parameter name
value: Chinese character description and explanation of the
parameter required: whether the parameter must be passed
paramType: Where to put the parameter
· header --> get request parameter: @RequestHeader
· query --> get request parameter: @RequestParam
· path (for restful interface) --> get request parameter: @PathVariable
· div (Not commonly used)
· form (not commonly used)
dataType: parameter type, default String, other values ​​dataType="Integer"
defaultValue: default value of the parameter

@ApiResponses: used in the request method to represent a set of responses
@ApiResponse: used in @ApiResponses, generally used to express an incorrect response message
code: number, such as 400
message: information, such as "request parameters are not filled in"
response: the class that threw the exception

@ApiModel: Used on the response class to indicate a message that returns the response data
(this is generally used in scenarios such as @RequestBody when the post is created, when the
request parameters cannot be described using the @ApiImplicitParam annotation)
@ApiModelProperty: Use On attributes, describe the attributes of the response class

Guess you like

Origin blog.csdn.net/qq_36008278/article/details/114366566