springboot swagger配置,Unable to infer base url,拦截器问题

swagger配置很简单,但是,因为使用到了拦截器,所以,就不简单了,刚开始怎么也不能显示接口列表,后来才发现是拦截器的问题,然后就各种方法的试试。刚开始是配置webmvc的静态资源类来过滤,但是后来发现还是不行。

后来仔细想想,配置了静态资源过滤,但是拦截器与这个什么webmvcconfigureadpter 是没有关系的,虽然配置了,也还是会拦截。然后,就放弃了webmvc适配器实现的方式,在拦截器里面直接排除,然后就可以了。历程如下。

1、引入swagger依赖 pom.xml 中

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>2.7.0</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>2.7.0</version>

</dependency>

我的项目必须使用2.7,我也不知道为啥试了其它的都不行。看看你们的喽。

2、添加配置

@Configuration

@EnableSwagger2

public class Swagger2Config {

@Bean

public Docket createRestApi() {

return new Docket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo())

.select()

//加了ApiOperation注解的类,才生成接口文档

.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))

//包下的类,才生成接口文档

//.apis(RequestHandlerSelectors.basePackage("site.haiyang.cms.controller"))

.paths(PathSelectors.any())

.build();/*

.securitySchemes(security());*/

}

private ApiInfo apiInfo() {

return new ApiInfoBuilder()

.title("Spring Boot 测试使用 Swagger2 构建RESTful API")

.contact(new Contact("haiyangsite", "http://haiyang.site", ""))

.version("1.0")

.description("API 描述")

.build();

}

}

3、配置具体接口上的注解数据:

@RequestMapping("updateUser")

@ResponseBody

@ApiOperation(value = "修改用户" , notes="更新用户信息")

@ApiImplicitParams({

@ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "query", dataType = "String"),

@ApiImplicitParam(name = "nickName", value = "用户昵称", required = true, paramType = "query", dataType = "String")

})

public String update(HttpServletRequest request, String phone,String nickName)

4、重启springboot服务,浏览器键入http://localhost:8080/swagger-ui.html 测试是否成功,如果显示了你配置的api,就成功了。如果没有显示,就可能是被拦截了。我的就是。接下来说说我的遇到的问题及解决办法。

5、为了测试,我让拦截器始终返回true,然后,键入地址,奇迹般的变化了,但是,仍然遇到了错误。。。页面alert出了这个Unable to infer base url错误提示,然后就怀疑是不是swagger配置错了,其实不是,还是拦截器的问题,因为我虽然返回了true,但是我的true里还有response.writer 对象往页面写东西,这个是没法改动的,所以,我就又想办法,试着过滤掉静态资源文件,于是,进行了以下配置:

@Configuration

public class WebMvcConfig extends WebMvcConfigurerAdapter {

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

registry.addResourceHandler("/swagger/**").addResourceLocations("classpath:/static/swagger/");

}

}

然后进行测试,还是不行,然后通过浏览器调试,发现是一个swagger-resources路径的被拦截了,我承认我这里是没有拦截,但是也懒得理会了。

6、最后,我直接在拦截器中统一过滤掉所有文件,就好了:

registry.addInterceptor(getInterfaceAuthCheckInterceptor()).addPathPatterns("/**")

//这个是为了swagger做的过滤

.excludePathPatterns("/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg", "/*.html", "/**/*.html","/swagger-resources/**")

7、好了,现在可以看到接口列表了:

8、结语

naquanjie.com 一个免费领券的网站,ilujune,一个免费查券的微信号机器人

http://static.music.haiyang.site/music.html 欢迎大家来听歌哟(^U^)ノ~YO

如有任何疑问,请联系183942498 微信qq同号。谢谢您~

猜你喜欢

转载自blog.csdn.net/oceanyang520/article/details/83120411