使用swagger出现Unable to infer base url. This is common when using dynamic servlet...

解决这样情况的方案:

<!--导入swagger2依赖-->
  <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>

创建配置类SwaggerConfig(这里需要手动创建,不要复制):

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    //配置Swagger的Docket的bean实例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("tom")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.alibaba.seata.controller"))
                // .paths(PathSelectors.ant("/hello/**"))
                .build();
    }
    //配置swagger的信息APIinfo
    public ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("tom", "https://mail.qq.com/", "1554289942@qq.com");
        return new ApiInfo(
                "Tom的Swagger API文档",
                "学习Java是没有前途的",
                "1.0",
                "https://mail.qq.com/",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>());

    }
}

然后启动项目,在地址栏输入http://localhost:8300/swagger-ui.html

上一篇:最新版Swagger 3升级指南和新功能体验!


下一篇:Spring Boot 集成 Swagger 文档的代码实现~