我在我的spring boot应用中使用了swagger 2.9.2。
localhost:8080/api-docs工作得很好。
但是,localhost:8080/swagger-ui.html返回writelabel error。
localhost:8080/v2/swagger-ui.html和localhost:8080/api/swagger-ui.html返回相同的错误。我一定漏掉了一些简单的东西。谢谢。
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Aug 22 10:05:48 CDT 2018
There was an unexpected error (type=Not Found, status=404).
No message available在build.gradle中,我依赖于springfox。
compile("io.springfox:springfox-swagger2:2.9.2")
compile("io.springfox:springfox-swagger-ui:2.9.2")
swaggerconfig.java
@Configuration
@EnableSwagger2
public class SwaggerConfig{
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage(MyServiceController.class.getPackage().getName()))
//.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.ant("/api/**"))
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
String description = "Company - My API";
return new ApiInfoBuilder()
.title("REST API")
.description(description)
.version("1.0")
.build();
}
MyServiceController.java
@ApiOperation(value = "some description",
response = MyServiceResponse.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "ok"),
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 401, message = "not authorized"),
@ApiResponse(code = 403, message = "not authenticated"),
@ApiResponse(code = 404, message = "The resource you were trying to reach is not found"),
@ApiResponse(code=500, message = "Interval Server Error")
})
@RequestMapping(method = RequestMethod.POST, value = "/api/component/operation", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@ResponseBody
{
do something
}发布于 2019-08-05 19:07:07
@Configuration @EnableSwagger2公共类SwaggerConfig实现WebMvcConfigurer { //您的摘要和ApiInfo方法@Override public void addResourceHandlers(webjars注册表){ registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/");}
只是试着添加它,并与您分享发生了什么。
发布于 2018-08-23 17:13:56
如下所示返回Docket bean:
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}并在控制器类之上添加@RestController注释
发布于 2020-05-17 21:53:06
So if u have correctly written the swaggerConfig code also added the right dependencies and still getting error
The ultimate solution is
You need to have perfect combination of swagger version and spring boot version
Just change the spring boot and swagger version as below
Check in your pom.xml or gradle build
Spring boot version :- <version>1.4.1.RELEASE</version>
Swagger and Sawgger ur version:- <version>2.2.2</version>
There are other combinations available but that u need to try on trial basishttps://stackoverflow.com/questions/51969894
复制相似问题