SpringBoot configuration SwaggerUI access report 404 error

Reprinted at: https://blog.csdn.net/hwangfantasy/article/details/66542602

 

SpringBoot configures SwaggerUI to access the 404 pit

When learning SpringBoot to build Restful API, I encountered a small pit, and I couldn't access it when configuring Swagger UI.

First, add the dependency of Swagger in your pom file, as shown below:

        <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>

Then create a new SwaggerConfig class:

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();
    }
}

Finally, add a series of API annotations to your Controller. In fact, it can be used normally without adding API annotations.
Finally, visit localhost:8080/swagger-ui.html to see the swagger page.

But the key is here. The first time I configured it in this way, the following error was prompted:

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

But I have a new project to reconfigure without any problem, so think of your own projects certainly what configuration and swagger conflict, and
finally found application.properties put in

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

Comment out this line to access it.

 

Supplement the second method:

Look at the structure of the project's dependency on swagger:

You can see that swagger-ui.html is in the META-INF/resources directory, so we need to manually point the static resource path to here, and configure it in java as:

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

    }
}

Inherit WebMvcConfigurationSupport in the configuration class of swagger, implement the addResourceHandlers method, and set static resources to be accessible.

After the setting is complete, restart the project, and you can access it normally through http://localhost:8080/swagger-ui.html.

 

Guess you like

Origin blog.csdn.net/My_SweetXue/article/details/114113815