SpringBoot工程搭建
创建 Maven 工程
参考:https://www.cnblogs.com/TangXiaoHuiHui/articles/12264019.html 进行创建
添加相关依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<hutool.version>5.7.8</hutool.version>
<swagger-ui.version>2.9.2</swagger-ui.version>
<swagger2.version>2.9.2</swagger2.version>
<swagger-bootstrap-ui.version>1.8.5</swagger-bootstrap-ui.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>7.16.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger-ui.version}</version>
</dependency>
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger2.version}</version>
</dependency>
<!-- 引入swagger-bootstrap-ui包 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>${swagger-bootstrap-ui.version}</version>
</dependency>
</dependencies>
添加启动类
/**
* @author BNTang
* @version V1.0
* @project SpringBoot-ElasticSearch-Pro
* @date Created in 2022/3/5 /005 15:21
* @description 启动类
**/
@SpringBootApplication
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class ElasticSearchApplication {
public static void main(String[] args) {
SpringApplication.run(ElasticSearchApplication.class, args);
}
}
配置 swagger
/**
* @author BNTang
* @version V1.0
* @project SpringBoot-ElasticSeach-Pro
* @date Created in 2022/3/5 /005 15:19
* @description swagger 相关配置
**/
@Configuration
public class SwaggerConfig {
@Bean
public Docket createH5RestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("elasticSearch api文档")
.description("restful 风格接口")
.version("1.0")
.build();
}
}
到了这一步,博主在启动项目的时候发现报错了:
居然原因呢,是因为博主这里用的 SpringBoot 版本是最新的,最近对 Spring Boot 2.6.0 的文档进行研究发现,2.6.0 开始使用基于 PathPatternParser
的路径匹配,而 Springfox 版本一直没有更新还是使用的 AntPathMatcher
导致了这个问题,要处理问题也很简单,修改 yaml
文件,将 SpringBoot
路劲匹配模式修改为 AntPathMatcher
就可以了,配置如下:spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
。
创建 yml 配置文件
application.yml:
spring:
mvc:
pathmatch:
matching-strategy: ANT_PATH_MATCHER
然后再次启动再浏览器当中访问:http://localhost:8080/doc.html