(1)主pom导包(子pom要引用,可选依赖)
<!-- swagger3用-->
<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>
(2)配置swagger和mvc
server:
port: 8081
spring:
# swagger使用
mvc:
path-match:
matching-strategy: ant_path_matcher
datasource:
# 指定连接池的类型
type: com.alibaba.druid.pool.DruidDataSource
druid:
url: "jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8&useSSL=false&useUnicode=true&serverTimezone=Asia/Shanghai"
username: "root"
password: "lht660036"
max-active: 20
# ???
# wall ??druid ???
# stat ??druid?????
filters: "wall,slf4j,stat"
web-stat-filter:
enabled: true
url-pattern: /*
# ??????????
exclusions: "/druid,*.png"
# ???????
stat-view-servlet:
enabled: true
url-pattern: "/druid/*"
reset-enable: true
login-username: admin
login-password: admin
mybatis:
# ??mapper.xml
mapper-locations: "classpath*:mapper/**/*.xml"
# ???
type-aliases-package: "com.lht.project.data.mybatis.entity"
swagger:
base-package: "com.lht.project.data.mybatis.controller"
title: "京东商城在线Api文档"
group-name: "订单商城"
description: "出现bug,请熟读该文档"
name: "刘惠棠帅哥"
url: "https://www.baidu.com"
email: "11111@163.com"
version: "1.0.0"
package com.lht.project.data.mybatis.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("swagger")
@Data
public class SwaggerProperties {
private String title;
private String basePackage;
private String groupName;
private String description;
private String name;
private String url;
private String email;
private String version;
}
(4)定义swaggerConfig
package com.lht.project.data.mybatis.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import javax.annotation.Resource;
@Configuration
@EnableOpenApi
public class SwaggerConfig {
@Resource
private SwaggerProperties swaggerProperties;
@Bean
public Docket docket(ApiInfo apiInfo){
return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo)
.groupName(swaggerProperties.getGroupName())
.select()
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
.paths(PathSelectors.any())
.build();
}
@Bean
public ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title(swaggerProperties.getTitle())
.description(swaggerProperties.getDescription())
.version(swaggerProperties.getVersion())
.contact(new Contact(swaggerProperties.getName(), swaggerProperties.getUrl(), swaggerProperties.getEmail()))
.build();
}
}
(5)controller调用