在维护开源项目:https://springboot.plus 时,使用SpringBoot依赖的版本是2.1.6.RELEASE,Swagger依赖版本是2.6.1,发现一个问题,在SpringBoot2.1.6集成Swagger2.6.1后,无法正常访问在src/main/resources/目录下 static文件夹中的 静态资源文件
一、引入Maven依赖如下:
io.springfox
springfox-swagger2
${swagger2.version}
io.springfox
springfox-swagger-ui
${swagger2.version}
二, 编写配置文件 :
/**
* Copyright 2019-2029 geekidea(https://github.com/geekidea)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.geekidea.springbootplus.config;
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.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
/**
* Swagger2全局配置
* @author geekidea
* @date 2018-11-08
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
/**
* 标题
*/
@Value("${swagger.title}")
private String title;
/**
* 基本包
*/
@Value("${swagger.base.package}")
private String basePackage;
/**
* 描述
*/
@Value("${swagger.description}")
private String description;
/**
* URL
*/
@Value("${swagger.url}")
private String url;
/**
* 作者
*/
@Value("${swagger.contact.name}")
private String contactName;
/**
* 作者网址
*/
@Value("${swagger.contact.url}")
private String contactUrl;
/**
* 作者邮箱
*/
@Value("${swagger.contact.email}")
private String contactEmail;
/**
* 版本
*/
@Value("${swagger.version}")
private String version;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage))
.paths(PathSelectors.any())
.build()
.globalOperationParameters(setHeaderToken());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(title)
.description(description)
.termsOfServiceUrl(url)
.contact(new Contact(contactName,contactUrl,contactEmail))
.version(version)
.build();
}
private List setHeaderToken() {
List pars = new ArrayList<>();
// TODO 测试token值,上线关闭
String testTokenValue = "";
ParameterBuilder tokenPar = new ParameterBuilder();
Parameter tokenParameter = tokenPar.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).defaultValue(testTokenValue).build();
pars.add(tokenParameter);
return pars;
}
}
到目前已经配置完成, 启动项目访问: http://localhost:${port}/${context-path}/swagger-ui.html
如果访问成功,则不需要配置,如果访问失败, 报错404, 则进行下面配置:
三, 解决404报错:
/**
* Copyright 2019-2029 geekidea(https://github.com/geekidea)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.geekidea.springbootplus.config;
import io.geekidea.springbootplus.common.web.interceptor.PermissionInterceptor;
import io.geekidea.springbootplus.common.web.interceptor.TokenTimeoutInterceptor;
import io.geekidea.springbootplus.config.core.SpringBootPlusProperties;
import io.geekidea.springbootplus.security.interceptor.JwtInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author geekidea
* @date 2018-11-08
*/
@Configuration
@Slf4j
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private SpringBootPlusProperties springBootPlusProperties;
@Autowired
private JwtInterceptor jwtInterceptor;
@Autowired
private PermissionInterceptor permissionInterceptor;
@Autowired
private TokenTimeoutInterceptor tokenTimeoutInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// // JWT拦截器
// registry.addInterceptor(jwtInterceptor)
// .addPathPatterns("/**")
// .excludePathPatterns(springBootPlusProperties.getInterceptorConfig().getJwtConfig().getExcludePath());
//
// // TOKEN超时拦截器
// registry.addInterceptor(tokenTimeoutInterceptor)
// .addPathPatterns("/**")
// .excludePathPatterns(springBootPlusProperties.getInterceptorConfig().getTokenTimeoutConfig().getExcludePath());
//
// // 权限拦截器
// registry.addInterceptor(permissionInterceptor)
// .addPathPatterns("/**")
// .excludePathPatterns(springBootPlusProperties.getInterceptorConfig().getPermissionConfig().getExcludePath());
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");//解决访问静态资源404
registry.addResourceHandler("swagger-ui.html") //解决访问swagger资源404
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**") //解决访问swagger资源404
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
项目结构:
访问swagger-ui.html:http://localhost:8888/swagger-ui.html#/
访问静态资源文件:http://localhost:8888/static/images/D.jpg