英文:
Whitelabel Error Page Problem accessing Swagger
问题
访问Swagger文档时,我遇到了这个错误:
白标签错误页面 此应用程序没有对 /error 的显式映射,因此您正在看到它作为回退。
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());
}
}
英文:
When trying to access the Swagger documentation, I face this error:
> Whitelabel Error Page This application has no explicit mapping for
> /error, so you are seeing this as a fallback.
>
> Wed Sep 23 09:38:17 BRT 2020 There was an unexpected error (type=Not
> Found, status=404). No message available
I don't know what can be...
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>
Class configuration:
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();
}
}
Controller:
@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());
}
答案1
得分: 0
请添加这些配置:
@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/
英文:
Please add those configs
@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/");
}
This must be declareted in the config of implementation with "WebMvcConfigurer".
More information here : https://springfox.github.io/springfox/docs/current/
答案2
得分: 0
对于使用 Spring Boot 和 SpringFox 3,您只需以下内容:
<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
。来自文档的说明:
swagger-ui 的位置已从 http://host/context-path/swagger-ui.html 移动到 http://host/context-path/swagger-ui/index.html 或 http://host/context-path/swagger-ui/(缩写)
英文:
For Spring Boot with SpringFox 3 all you need is following:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
and /swagger-ui.html
is now /swagger-ui/index.html
or /swagger-ui
. From doc
> swagger-ui location has moved from http://host/context-path/swagger-ui.html to http://host/context-path/swagger-ui/index.html OR http://host/context-path/swagger-ui/ for short
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论