SpringBoot構成SwaggerUIアクセスレポート404エラー

転載:https//blog.csdn.net/hwangfantasy/article/details/66542602

 

SpringBootは、404ピットにアクセスするようにSwaggerUIを構成します

SpringBootを学習してRestfulAPIを構築するときに、小さな穴に遭遇し、SwaggerUIを構成するときにそれにアクセスできませんでした。

まず、以下に示すように、pomファイルにSwaggerの依存関係を追加します。

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>

次に、新しいSwaggerConfigクラスを作成します。

Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.nightowl"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("NightOwl RESTful APIs")
                .description("关注我 http://hwangfantasy.github.io/")
                .termsOfServiceUrl("http://hwangfantasy.github.io/")
                .contact("颜艺学长")
                .version("1.0")
                .build();
    }
}

最後に、一連のAPIアノテーションをコントローラーに追加します。実際、APIアノテーションを追加しなくても通常どおり使用できます。
最後に、localhost:8080 / swagger-ui.htmlにアクセスして、Swaggerページを表示します。

しかし、鍵はここにあります。この方法で初めて構成したときに、次のエラーが表示されました。

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Nov 24 19:57:13 CST 2016
There was an unexpected error (type=Not Found, status=404).
No message available

しかし、私はそうあなた自身のプロジェクトを考える確かにどのような設定と闊歩の競合、及び、何の問題もなく再構成するために、新しいプロジェクトを持って
最終的に見つけapplication.propertiesを入れ

spring.resources.static-locations=classpath:/static/

この行をコメントアウトしてアクセスします。

 

2番目の方法を補足します。

プロジェクトのSwaggerへの依存の構造を見てください。

swagger-ui.htmlがMETA-INF / resourcesディレクトリにあることがわかるので、静的リソースパスを手動でここにポイントし、Javaで次のように構成する必要があります。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
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;

/**
 * @author xiaqing
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xqnode.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口总览")
                .description("测试")
                .version("1.0")
                .build();
    }

    /**
     * 防止@EnableMvc把默认的静态资源路径覆盖了,手动设置的方式
     *
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决静态资源无法访问
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        // 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

    }
}

swaggerの構成クラスでWebMvcConfigurationSupportを継承し、addResourceHandlersメソッドを実装し、静的リソースにアクセスできるように設定します。

設定が完了したら、プロジェクトを再起動します。通常、http:// localhost:8080 /swagger-ui.htmlからプロジェクトにアクセスできます。

 

おすすめ

転載: blog.csdn.net/My_SweetXue/article/details/114113815