springboot 2.0.4 利用swagger2构建api文档

时间:2021-04-30 05:40:21
springboot 2.0.4
swagger 2.8.0
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>

 

sp升级到2.0 以后,swagger 包也要升级,否则页面打不开

一、pom

二、 配置类

 

package cn.com.connext.authority.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /**
     * 可以定义多个组,比如本类中定义把test和demo区分开了
     * (访问页面就可以看到效果了)
     */
    @Bean
    public Docket testApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("auth")
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .pathMapping("/")// base,最终调用接口后会和paths拼接在一起
                .select()
                .paths(or(regex("/api/.*")))//过滤的接口
                .build()
                .apiInfo(testApiInfo());
    }


    private ApiInfo testApiInfo() {
        return new ApiInfoBuilder()
                .title("product API")//大标题
                .version("1.0")//版本
                .build();
    }

}

 

访问:

http://localhost:9091/swagger-ui.html

springboot 2.0.4 利用swagger2构建api文档

 

swagger2.9 好像又不一样了

参考:https://blog.csdn.net/allen_lu_hh/article/details/83218028

404 参考 https://www.jianshu.com/p/04dd5ff82dad