Spring Boot 整合 Swagger3
导入依赖
<!--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>
application.yml配置
spring:
application:
name: springboot01_03
#修改SpringMVC路径匹配规则(swagger)
mvc:
pathmatch:
matching-strategy: ant_path_matcher
datasource:
url: jdbc:mysql://localhost:3306/shiro_jwt?useSSL=true&useUnicode=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: zz2232996
driver-class-name: com.mysql.cj.jdbc.Driver
#Swagger
knife4j:
enable: true
Swagger配置类
package com.example.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;
/**
* Swagger配置类
*/
@Configuration
@EnableOpenApi//Swagger 开启生成接口文档功能
public class SwaggerConfig {
/**
* application中还配置了mvc,因为springboot与swagger3不兼容
*/
/**
* ture 启用Swagger3.0, false 禁用(生产环境要禁用)
*/
//是否开启swagger,正式环境一般是需要关闭的,可根据springboot的多环境配置进行设置
@Value("${knife4j.enable}")
private Boolean swaggerEnabled;
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
// 是否开启
.enable(swaggerEnabled)
.select()
// 扫描的路径使用@Api的controller
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// 指定路径处理PathSelectors.any()代表所有的路径
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("接口文档标题")
.description("接口文档描述")
//作者信息
.contact(new Contact("作者","作者URL","作者Email"))
.version("版本")
.build();
}
}
UserEntity
package com.example.mp.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author Yu
* @since 2022-10-08 22:35:36
*/
@Getter
@Setter
@Accessors(chain = true)
@TableName("user")
@ApiModel(value = "UserEntity对象", description = "用户表")
public class UserEntity {
// value–字段说明
// name–重写属性名字
// dataType–重写属性类型
// required–是否必填
// example–举例说明
// hidden–隐藏
@TableId(value = "id", type = IdType.AUTO)
@ApiModelProperty(value = "id")
private Integer id;
@TableField("uname")
@ApiModelProperty(value = "姓名")
private String uname;
@TableField("`password`")
@ApiModelProperty(value = "密码")
private String password;
@TableField("salt")
@ApiModelProperty(value = "盐")
private String salt;
}
@ApiModel:在实体类上边使用,标记类时swagger的解析类
@ApiModelProperty :用于方法,字段; 表示对model属性的说明或者数据操作更改
UserController
package com.example.mp.controller;
import com.example.mp.entity.UserEntity;
import com.example.mp.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* <p>
* 前端控制器
* </p>
*
* @author Yu
* @since 2022-10-08 22:35:36
*/
@RestController
@RequestMapping("/mp/user-entity")
@Api(tags = "UserController接口")
public class UserController {
@Autowired
UserService userService;
@GetMapping("list")
@ApiOperation(value = "查看所有用户")
public List<UserEntity> list(){
List<UserEntity> list = userService.list();
return list;
}
}
输入网址
💡 http://localhost:端口号/doc.html