【问题标题】:Whitelabel Error Page Problem accessing SwaggerWhitelabel 错误页面问题访问 Swagger
【发布时间】:2020-09-23 13:05:10
【问题描述】:

在尝试访问 Swagger 文档时,我遇到了这个错误:

Whitelabel 错误页面 此应用程序没有明确的映射 /error,因此您将其视为后备。

Wed Sep 23 09:38:17 BRT 2020 There was an unexpected error (type=Not 找到,状态=404)。没有可用的消息

我不知道会是什么......

pom.xml:

<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 com.simulacao.api.config;

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

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket bancoApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.simulacao.api"))
                .paths(PathSelectors.regex("/*.*"))
                .build()
                .apiInfo(metaInfo());
    }

    private ApiInfo metaInfo() {
        return new ApiInfoBuilder()
                .title("API Rest")
                .description("\"API Rest\"")
                .version("1.0.0")
                .license("Apache License Version 2.0")
                .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0\"")
                .build();
    }
}

控制器:

@RestController
@RequestMapping(value = "/api/customer")
@Api(value = "API Rest Customer")
@CrossOrigin(origins = "*")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private CustomerRepository customerRepository;

    @GetMapping
    @ApiOperation(value = "Returns a list of customers")
    public ResponseEntity<List<Customer>> showAll() {
        return ResponseEntity.ok(this.customerService.findAll());
    }

【问题讨论】:

    标签: java spring-boot swagger


    【解决方案1】:

    请添加这些配置

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
    
        //Swagger UI property
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
    
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
    

    这必须在实现的配置中用“WebMvcConfigurer”声明。

    更多信息在这里:https://springfox.github.io/springfox/docs/current/

    【讨论】:

      【解决方案2】:

      对于带有 SpringFox 3 的 Spring Boot,您只需要以下内容:

          <dependency>
              <groupId>io.springfox</groupId>
              <artifactId>springfox-boot-starter</artifactId>
              <version>3.0.0</version>
          </dependency>
      

      /swagger-ui.html 现在是/swagger-ui/index.html/swagger-ui。来自doc

      swagger-ui 位置已从 http://host/context-path/swagger-ui.html 移动到 http://host/context-path/swagger-ui/index.html 或 http://host/简称context-path/swagger-ui/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-23
        • 2019-07-17
        • 2023-03-13
        • 1970-01-01
        • 2017-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多