导航

  • 官方start包(推荐)
  • 引入pom
  • 老版本自己引入springfox包
  • 导包
  • 编写swagger配置文件
  • 报错
  • 成功


官方start包(推荐)

引入pom

2020.7月份出的:

springboot整合swagger_html

<!--springfox swagger官方Starter-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

配置文件还是一样的,如果老的项目配置了,就只需要把包引入改掉就好了:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.study.springbootplus"))
        //包下的类,生成接口文档
        .paths(PathSelectors.any())
        .build();
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("yida接口文档")
        .description("yida接口文档")
        .version("1.0.0")
        .build();
  }

}

2020.11.18,项目一开始可以直接访问,后面非要加上注解@EnableOpenApi才可以访问,不知道为什么?

2020.11.24试了一下,访问http://localhost:8080/swagger-ui的时候就不行,访问地址最后非要加上/不知道为什么…

错误访问地址http://localhost:8080/swagger-ui
访问地址:http://localhost:8080/swagger-ui/

老版本自己引入springfox包

导包

首先确保引入了包:
版本自己可以去种牙仓库上面看看:中央仓库

<!-- swagger2-UI-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

编写swagger配置文件

配置文件的SWAGGER_SCAN_BASE_PACKAGE 路径一定要注意
然后controller里面一定要有一个

@Api(tags = "TestController", description = "测试controller")

要不然都是空的,肯定不行的…

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.mr.web.controller";
    public static final String VERSION = "1.0.0";

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("web")
                .description("This is to show api description")
                .license("Apache 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .termsOfServiceUrl("")
                .version(VERSION)
                .contact(new Contact("", "", "Stack@163.com"))
                .build();
    }

    @Bean
    public Docket customImplementation() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
                .paths(PathSelectors.any())
                .build();
    }

}

然后注意一定要swagger被扫描到

报错

我端口弄成的80,所以路径如下:
http://localhost/swagger-ui.html#/ No mapping for GET /swagger-ui.html
启动之后,访问不到,想起来,swagger需要访问静态资源,但是我们没有配置,那就配置静态资源映射

@Configuration
@EnableWebMvc
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/");

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登录拦截
        registry.addInterceptor(new LoginInterceptor());
    }

}

成功

再次启动:访问http://localhost/swagger-ui.html#/成功,

springboot整合swagger_html_02