swagger-ui 在Spring boot项目中报错Unable to infer base url

9 篇文章 0 订阅
Swagger-UI现在在Spring Boot项目中引用非常的简单
1. pom.xml中引入dependency,注意版本兼容性
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>

2.Main方法启动参数中加入注解 @EnableSwagger2
3.@Configuration注解的类,或者是Main方法类中加入Docket Bean:

@Bean
public Docket createRestApi() {
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
tokenPar.name("token").description("Token").modelRef(new ModelRef("string")).parameterType("header")
.required(false).build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.iteye.wwwcomy.controller")).paths(PathSelectors.any())
.build().globalOperationParameters(pars);
}

private ApiInfo apiInfo() {
Contact contact = new Contact("wwwcomy", "", "wwwcomy@gmail.com");
return new ApiInfoBuilder().title("Web Diary").description("Web Diary").contact(contact).version("1.0").build();
}


启动之后访问swagger ui默认地址 http://localhost:8080/swagger-ui.html即可

如果访问后出现弹出框 提示Unable to infer base url XXX,表面意思上好像是Base url的问题。 如果确认自己确实没有使用context url的话,看一下自己的spring security是不是启用了,如果启用的话会被拦截掉, swagger ui会误报成这个错误。 把swagger ui资源排除在外的配置:

@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security", "/swagger-ui.html", "/webjars/**");
}

}


我本地是把securiy先禁用了,application.properties设置
security.basic.enabled=false
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值