springboot整合swagger3和knife4j

依赖

 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.3</version>
</dependency>

application.yml配置

  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher ## 解决Failed to start bean ‘documentationPluginsBootstrapper‘ 问题
knife4j:
  enable: true

swagger配置类

package com.example.springbooth2.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableOpenApi//Swagger 开启生成接口文档功能
public class SwaggerConfig {
    
    

    /**
     * ture 启用Swagger3.0, false 禁用
     */
    @Value("${knife4j.enable}")
    private Boolean enable;

    @Bean
    Docket docket() {
    
    
        return new Docket(DocumentationType.OAS_30)
                //配置网站的基本信息
                .apiInfo(apiInfo())
                .enable(enable)
                .select()
                //指定接口的位置
                // RequestHandlerSelectors.basePackage("com.example.springbooth2.controller") 接口的包所在路径
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 指定路径处理PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
    
    
        return new ApiInfoBuilder()
                .title("swagger3") //接口文档标题
                .description("接口文档") //接口文档描述
                //作者信息
                // new Contact("作者","作者URL","作者Email")
                .contact(new Contact("jane", "http://www.baidu.com", "[email protected]"))
                .version("1.0")//版本
                .build();
    }
}

controller

package com.example.springbooth2.controller;

import com.example.springbooth2.config.ResponseResult;
import com.example.springbooth2.entity.User;
import com.example.springbooth2.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/user")
public class UserController {
    
    

    @Resource
    private UserService userService;

    @ApiOperation("添加用户")
    @ApiImplicitParams({
    
     // 参数名称
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "userName", value = "用户名",  required = true)
    })
    @PostMapping("add")
    public User add(User user) {
    
    
        userService.addUser(user);
        return user;
    }

    @ApiOperation("查询所有用户")
    @GetMapping("list")
    public ResponseResult<User> list() {
    
    
        return ResponseResult.success(userService.list());
    }

    @GetMapping("/findOne")
    public ResponseResult<User> findOne() {
    
    
        return ResponseResult.success(userService.findById(1));
    }
}

测试

knife4j访问地址 http://localhost:9090/doc.html
insert image description here

swagger访问地址 http://localhost:9090/swagger-ui/index.html
insert image description here

swagger常用注解说明

@Api

用在请求的类上
@Api(tags = “该类的作用进行说明”)

@ApiOperation

@ApiOperation(value=“改方法的作用”,note=“备注”)

@ApiImplicitParams

@ApiImplicitParams: Used in the request method, indicating a set of parameter description
@ApiImplicitParam: Used in the @ApiImplicitParams annotation, specifying 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 --> request parameter acquisition: @RequestHeader
query --> request parameter acquisition: @RequestParam
path (for restful interface) –> request parameter acquisition: @PathVariable
dataType: Parameter type, default String, other values ​​dataType=“Integer”
defaultValue: the default value of the parameter
Multiple @ApiImplicitParam annotations need to be placed in one @ApiImplicitParams annotation
Although the @ApiImplicitParam annotation can specify that the parameter is required, it cannot replace @ RequestParam(required = true) ,
the former is required only in the Swagger framework. If Swagger is discarded, this restriction is useless, so if the developer needs to specify a parameter that is required, the @RequestParam(required = true) annotation is still Cannot be omitted.

@ApiModel

If the parameter is an object (such as the update interface above), the description of the parameter can also be placed in the entity class. For example, the following piece of code
@ApiModelProperty: used on attributes to describe the attributes of the class

@ApiModel(value = "用户对象",description = "用户表")
public class User {
    
    
    @Id
    @ApiModelProperty(value = "id",required = true)
    private int userId;
    @ApiModelProperty(value = "用户名称",required = true)
    private String userName;
}

insert image description here

Guess you like

Origin blog.csdn.net/Java_Fly1/article/details/127751010