首页 > 解决方案 > Swagger UI 不适用于 JSON 配置

问题描述

我已经删除了 @EnableSwagger2 & io.springfox:springfox-swagger2 依赖项,以便不使用传统的 swagger 相关注释,而是使用 JSON 文件。

我已使用此处共享的详细信息将 Swagger 从基于注释的注释更改为基于 JSON:Overcoming Swagger Annotation Overload by Switching to JSON

但现在它显示错误

有人可以帮我解决可能导致问题的原因吗?

标签: javaspringswaggerswagger-ui

解决方案


我认为您没有在 Pom.xml 文件中添加 swagger 依赖项:

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.5.0</version>
        </dependency> 

我认为这可能是它没有打开 Swagger UI 的原因。请尝试在配置文件中创建bean,以便swagger可以正常运行。

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
                .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.cloud")))
                .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.data.rest.webmvc")))
                .paths(PathSelectors.any()).build();
    }}

推荐阅读