pom.xm里寫入swagger依賴:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Swaggerconfig類,代碼如下
import io.swagger.annotations.Api;
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.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swaggerconfig {
@Value("${swagger2.enable}")
private boolean enable;
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("學習項目")
.description("學習項目測試工具")
.contact("Lawrence")
.version("1.0")
.build();
}
@Bean("模塊一")
public Docket controller1Apis() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("模塊一")
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.regex("/controller1.*"))
.build()
.apiInfo(apiInfo())
.enable(enable);
}
@Bean("模塊二")
public Docket controller2Apis() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("模塊二")
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.regex("/controller2.*"))
.build()
.apiInfo(apiInfo())
.enable(enable);
}
}
因為使用了中文,有一些坑需要在application.yml文件里配置
server:
port: 9527
tomcat:
uri-encoding: UTF-8
max-threads: 800
min-spare-threads: 30
max-connections: 10000
servlet:
context-path: /lean
spring:
http:
encoding:
charset: UTF-8
force: true
enabled: true
swagger2:
enable: true
這樣多模塊配置就好了,訪問項目路徑/swagger-ui.html就ok,效果如下
注意,如果使用mybatis-plus的情況下會出現問題。原因是啟動類@ComponentScan導致swagger無法掃描到接口,改為@MapperScan就可以解決了。