Swagger2不显示界面和资源

img

img

其他都加载正常,就是不显示UI和Rescourse,社区里的办法都试过了。
为什么会出现这种情况?

swagger2的依赖是否加了?

题主 swagger2 版本会受 springboot 的影响,之前我也琢磨了很长时间选择swagger的版本,推荐你最好你选好了一个 swagger2 版本,后期就不要改动了,我目前也在用比较新的,还支持springboot2.1以上,你试试能不能访问。
之前我用的这两个,在springboot 1.5 用的还挺顺利的,但在此版本下,提高 swagger2 版本也会出现题主一样的问题。

springboot 1.5 ~ 2.1 版本用这个
访问路径:http://localhost:8080/swagger-ui.html

<!-- swagger2 配置 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>

参考配置:

package cn.fyupeng;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createWebApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("userApi")
                .apiInfo(webApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.fyupeng.controller.user"))
                //.paths(Predicates.and(PathSelectors.regex("/.*")))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket createAdminApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.fyupeng.controller.admin"))
                // .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                //设置页面标题
                .title("使用swagger2构建RPC分布式博客管理平台后端user-api接口文档")
                .contact(new Contact("distributed-blog-api - 仓库","git@github.com:fyupeng/distributed-blog-api/blob/main/README.md","fyp010311@163.com"))
                .description("欢迎访问RPC分布式博客管理平台接口文档,本文档描述了博客服务接口定义")
                .version("1.0.1")
                .build();
    }

    private ApiInfo adminApiInfo(){
        return new ApiInfoBuilder()
                //设置页面标题
                .title("使用swagger2构建RPC分布式博客管理平台后端admin-api接口文档")
                .contact(new Contact("distributed-blog-api - 仓库","git@github.com:fyupeng/distributed-blog-api/blob/main/README.md","fyp010311@163.com"))
                .description("欢迎访问RPC分布式博客管理平台接口文档,本文档描述了博客服务接口定义")
                .version("1.0.1")
                .build();
    }

}

2.1 版本及以上,不妨试试这个,页面展示也很新颖
访问路径右边,题主注意要用:http://localhost:8080/swagger-ui/index.html

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- 解决Double、Float类型转换失败问题 -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.22</version>
        </dependency>

具体配置参考:

package cn.fyupeng;

import cn.fyupeng.enums.RequestHeaderKey;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.*;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

@Configuration
//@EnableSwagger2
@EnableOpenApi
public class Swagger2 {

    @Bean
    public Docket createWebApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("userApi")
                .apiInfo(webApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.fyupeng.controller.user"))
                //.paths(Predicates.and(PathSelectors.regex("/.*")))
                .paths(PathSelectors.any())
                .build()
                .globalRequestParameters(getRequestParameter());
    }

    @Bean
    public Docket createAdminApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.fyupeng.controller.admin"))
                // .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
                .paths(PathSelectors.any())
                .build()
                .globalRequestParameters(getRequestParameter());
    }

    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                //设置页面标题
                .title("使用swagger2构建RPC分布式博客管理平台后端user-api接口文档")
                .contact(new Contact("distributed-blog-api - 仓库","git@github.com:fyupeng/distributed-blog-api/blob/main/README.md","fyp010311@163.com"))
                .description("欢迎访问RPC分布式博客管理平台接口文档,本文档描述了博客服务接口定义")
                .termsOfServiceUrl("https://www.zybuluo.com/mdeditor#2281023-full-reader")
                .version("1.0")
                .build();
    }

    private ApiInfo adminApiInfo(){
        return new ApiInfoBuilder()
                //设置页面标题
                .title("使用swagger2构建RPC分布式博客管理平台后端admin-api接口文档")
                .contact(new Contact("distributed-blog-api - 仓库","git@github.com:fyupeng/distributed-blog-api/blob/main/README.md","fyp010311@163.com"))
                .description("欢迎访问RPC分布式博客管理平台接口文档,本文档描述了博客服务接口定义")
                .termsOfServiceUrl("https://www.zybuluo.com/mdeditor#2281023-full-reader")
                .version("1.0")
                .build();
    }

    private List<RequestParameter> getRequestParameter() {
        //ParameterBuilder builder = new ParameterBuilder();
        RequestParameterBuilder builder = new RequestParameterBuilder();
        List<RequestParameter> pars = new ArrayList<>();
        builder.name(RequestHeaderKey.TOKEN_HEADER_KEY.getName())
                .description("token令牌")
                .in(ParameterType.HEADER)
                .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("1"))
                .build();
        pars.add(builder.build());
        return pars;
    }

第二个配置它不仅有 swagger2 的文档展示,其实也集成了多个 swagger,看题主想选择哪个,不过我还是选择 swagger 看起来更直观些。

img

希望对题主有帮助。

后续如有问题可继续在该评论反馈。