1、导入相关依赖
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!--swagger-ui-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
2、编写配置类
package com.qbb.config;
import io.swagger.v3.oas.annotations.Operation;
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;
/**
* @author QIUQIU&LL (个人博客:https://www.cnblogs.com/qbbit)
* @date 2022-10-25 20:52
* @tags 我爱的人在很远的地方, 我必须更加努力
*/
@Configuration
@EnableOpenApi// Swagger 开启生成接口文档功能
public class Swagger3Config {
/**
* application中还配置了mvc,因为springboot与swagger3不兼容
*/
/**
* ture 启用Swagger3.0, false 禁用(生产环境要禁用)
*/
// 是否开启swagger,正式环境一般是需要关闭的,可根据springboot的多环境配置进行设置
@Value("${knife4j.enable}")
private Boolean swaggerEnabled;
@Bean
public Docket webApiConfig() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(webApiInfo())
// 是否开启
.enable(swaggerEnabled)
.groupName("webApi")
.select()
// 扫描的路径使用@Api的controller
.apis(RequestHandlerSelectors.withMethodAnnotation(Operation.class))
// 指定路径处理 PathSelectors.any()代表所有的路径
//.paths(PathSelectors.any())
//只显示api路径下的页面
.paths(PathSelectors.regex("/api/.*"))
.build();
}
@Bean
public Docket adminApiConfig() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(adminApiInfo())
// 是否开启
.enable(swaggerEnabled)
.groupName("adminApi")
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(Operation.class))
//只显示admin路径下的页面
.paths(PathSelectors.regex("/admin/.*"))
.build();
}
private ApiInfo webApiInfo() {
return new ApiInfoBuilder()
.title("网站-API文档")
.description("本文档描述了网站微服务接口定义")
.version("1.0")
.contact(new Contact("QIUQIU&LL", "https://www.cnblogs.com/qbbit/", "[email protected]"))
.build();
}
private ApiInfo adminApiInfo() {
return new ApiInfoBuilder()
.title("后台管理-API文档")
.description("本文档描述了后台管理系统微服务接口定义")
.version("1.0")
.contact(new Contact("QIUQIU&LL", "https://www.cnblogs.com/qbbit/", "[email protected]"))
.build();
}
}
3、使用
package com.qbb.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qbb.common.R;
import com.qbb.entity.User;
import com.qbb.entity.vo.UserVo;
import com.qbb.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
/**
* @author QIUQIU&LL (个人博客:https://www.cnblogs.com/qbbit)
* @date 2022-10-25 20:52
* @tags 我爱的人在很远的地方, 我必须更加努力
*/
//@Api(tags = "用户管理")
@Tag(name = "用户管理")
@RestController
@RequestMapping("/admin/user")
@CrossOrigin
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
//@ApiOperation(value = "用户列表", notes = "用户列表")
@Operation(summary = "用户列表")
@GetMapping("/list/{pageNo}/{pageSize}")
public R page(
/*@ApiParam(value = "当前页", defaultValue = "1")*/
@Parameter(name = "pageNo", description = "当前页")
@PathVariable(value = "pageNo", required = false) Integer pageNo,
/*@ApiParam(value = "每页记录数", defaultValue = "10")*/
@Parameter(name = "pageSize", description = "每页记录数")
@PathVariable(value = "pageSize", required = false) Integer pageSize,
/*@ApiParam(value = "searchObj查询条件")*/
@Parameter(name = "userVo", description = "searchObj查询条件")
UserVo userVo) {
Page<User> page = new Page<>(pageNo, pageSize);
return R.ok().data(userService.pageList(page, userVo));
}
@Operation(summary = "保存用户")
@PostMapping("/save")
public R save(@Parameter(name = "user", description = "用户对象") @Valid @RequestBody User user) {
boolean flag = userService.save(user);
if (flag) {
return R.ok();
}
return R.error().message("保存失败!");
}
}
4、注意:启动项目会报错 ===> java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()" because "this.condition" is null
解决办法:在配置文件中加入以下配置即可
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher