maven项目集成Swagger2以及Swagger-UI.html提示Unable to infer base url...和不显示接口API问题解决方法

在pom.xml添加依赖

        <!--swagger依赖-->
        <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>

 编写Swagger2配置类

package com.study.swagger2;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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.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;

@Configuration//标注该类是个配置类
@EnableSwagger2//启用Swagger2
public class SwaggerConfig {

    private boolean enableSwagger;//是否启用Swagger

    public SwaggerConfig() {
        this.enableSwagger = true;
    }

    @Bean
    public Docket createRestApi() {
        //添加head请求头参数 start
        ParameterBuilder ticketPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        ticketPar.name("token").description("token")//name表示名称,description表示描述
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).defaultValue("").build();//required表示是否必填,defaultValue表示默认值
        pars.add(ticketPar.build());
        //end
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//设置文档信息
                .enable(enableSwagger)
                .select()//函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
                //此包路径下的类,才生成接口文档
                .apis(RequestHandlerSelectors.basePackage("com.study"))//这里采用包扫描的方式来确定要显示的接口
                //加了ApiOperation注解的方法,才生成接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))//这里采用包含注解的方式来确定要显示的接口
                .paths(PathSelectors.any())//返回包含所有满足条件的请求处理器的断言,该断言总为true
                .build()
                .globalOperationParameters(pars);//消息头添加
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("公共服务接口  APIs")//页面标题
                .description("")//描述
                .version("0.1.1")//版本
                .build();
    }
}

 在启动项目后,swagger-ui.html显示如下图

解决方法:

1,在swagger配置类添加@EnableWebMvc注解

@EnableWebMvc//开启对Web MVC 的配置支持
@Configuration//标注该类是个配置类
@EnableSwagger2//启用Swagger2
public class SwaggerConfig {

2,在spring配置文件添加 <context:component-scan/>扫描swagger配置类

    <!-- 扫描swagger配置文件  -->
    <context:component-scan base-package="com.study.swagger2"/>

再次启动项目后,显示如下图(别忘了在Controller类的方法上添加@ApiOperation注解)

不显示API接口解决方法: 

在swagger配置类添加@ComponentScan注解用来扫描接口所在包的位置

@ComponentScan("com.study")//需要扫描的api位置
@EnableWebMvc//开启对Web MVC 的配置支持
@Configuration//标注该类是个配置类
@EnableSwagger2//启用Swagger2
public class SwaggerConfig {

再次启动项目后显示如下图成功

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值