如何解决Whitelabel错误页面访问Swagger问题
当尝试访问Swagger文档时,我遇到此错误:
Whitelabel错误页面此应用程序没有针对 /错误,因此您将其视为备用。
2020年9月23日星期三09:38:17 BRT发生意外错误(类型=否 找到,状态= 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());
}
解决方法
请添加这些配置
@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/
,对于使用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 /简称
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。