记录404的一个坑
1、pom引入jar,最新的3.0.0版本会报404,有点Bug,所以选择2.9.2
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.13</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2、新建SwaggerConfig配置类
package com.llb.wxpaymentdemo.config;
import lombok.extern.slf4j.Slf4j;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
@Slf4j
public class Swagger2Config {
@Bean
public Docket docket(){
log.info("Swagger2Config init");
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(buildApiInfo())
.select() // 要扫描的API(Controller)基础包
.apis(RequestHandlerSelectors
.basePackage("com.llb.wxpaymentdemo.controller"))
.paths(PathSelectors.any())
.build();
// .apiInfo(new ApiInfoBuilder().title("微信支付案例接口文档").build())
// .genericModelSubstitutes(new Class[]{DeferredResult.class})
// .select().paths(PathSelectors.any())
// // 不显示错误的接口地址
.paths(Predicates.not(PathSelectors.regex("/error.*")))// 错误路径不监控
// .paths(PathSelectors.regex("/.*"))// 对根下所有路径进行监控
// .build()
// ;
}
private ApiInfo buildApiInfo() {
Contact contact = new Contact("开发者","","");
return new ApiInfoBuilder()
.title("闪聚支付‐商户应用API文档")
.description("") .contact(contact)
.version("1.0.0").build(); }
}
3.访问swagger
5 问题总结,如上访问一直404,尝试网上说的各种方法,添加WebMvcConfigurer配置addResourceHandlers都没用
最后发现问题是浏览器上,把浏览器上地址复制下来是:http://localhost:8089/swagger%E2%80%90ui.html
修改地址如下:
http://localhost:8001/swagger-ui.html
立马不再404了,所以根本不需要什么addResourceHandlers