首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swagger ui返回白色标签错误

Swagger ui返回白色标签错误
EN

Stack Overflow用户
提问于 2018-08-22 23:08:40
回答 5查看 5.3K关注 0票数 0

我在我的spring boot应用中使用了swagger 2.9.2。

localhost:8080/api-docs工作得很好。

但是,localhost:8080/swagger-ui.html返回writelabel error

localhost:8080/v2/swagger-ui.htmllocalhost:8080/api/swagger-ui.html返回相同的错误。我一定漏掉了一些简单的东西。谢谢。

代码语言:javascript
复制
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")

代码语言:javascript
复制
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        
    }
EN

回答 5

Stack Overflow用户

发布于 2019-08-05 19:07:07

  • 看起来你有必要的依赖,所以这不是问题所在。
  • 我认为你必须实现WebMvcConfigure和重写addResourceHandlers方法的问题:

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

只是试着添加它,并与您分享发生了什么。

票数 1
EN

Stack Overflow用户

发布于 2018-08-23 17:13:56

如下所示返回Docket bean:

代码语言:javascript
复制
@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
            .apiInfo(apiInfo());
}

并在控制器类之上添加@RestController注释

票数 0
EN

Stack Overflow用户

发布于 2020-05-17 21:53:06

代码语言:javascript
复制
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 basis
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51969894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档