背景说明
Springboot2.X暴露REST接口可以通过Swagger方式进行暴露方便接口调试。
解决方案
父POM
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
GAV引入
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.10.5</version>
</dependency>
默认首页
进入静态网页src/main/resources/static/index.html
编辑文件内容
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SwaggerUI</title>
</head>
<body>
<a href="/swagger-ui.html">swagger-ui view</a><br>
<a href="/doc.html">knife4j-ui view</a><br>
</body>
</html>
问题解决
访问首页后发现多出了控制器basic-error-controller
,此问题可以新增文件Swagger2Config
方案进行解决
package com.github.ljhan2.git.status.config;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* @author: ljhan2
* @date: 2020-07-05
*/
@Configuration
public class Swagger2Config {
@Bean
public Docket demoApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.paths(Predicates.not(PathSelectors.regex("/actuator.*")))
.build();
}
}
网友评论