遇到swagger不能显示问题
配置swagger
一级标题 引入pom依赖
引入pom依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger2.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger2.version}</version>
</dependency>
或者
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
二级标题 配置swagger的配置
@EnableSwagger2
@Configuration
public class Swagger2 {
@Bean
public Docket createRestApi() {
ParameterBuilder pb = new ParameterBuilder();
pb.name("FDToken").description("Token").modelRef(new ModelRef("string")).parameterType("header")
.required(false).defaultValue("Bearer ").build();;
List<Parameter> par = new ArrayList<Parameter>();
par.add(pb.build());//
return new Docket(DocumentationType.SWAGGER_2).enable(true).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.educiot.logistics.server.controller")).paths(PathSelectors.any())
.build().globalOperationParameters(par);
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("总务后勤api接口文档").version("1.0").build();
}
}
没有加入配置拦截器。swagger是正常的
二级目录 引入 WebMvcConfigurer
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Bean
public JwtInterceptor jwtInterceptor(){
return new JwtInterceptor();
}
/**
* 添加拦截器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
//拦截路径可自行配置多个 可用 ,分隔开
registry.addInterceptor(jwtInterceptor()).addPathPatterns("/**")
// 排除swagger
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**","/doc.html");
WebMvcConfigurer.super.addInterceptors(registry);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html", "doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
当没有加入 addResourceHandlers 会找不到swagger-ui 的地址。
addResourceHandlers 这是个坑。哈哈