使用Spring Boot配置Swagger时获得意外结果 [英] Getting an unexpected result while configuring Swagger with Spring Boot

查看:145
本文介绍了使用Spring Boot配置Swagger时获得意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Swagger非常陌生,因此我开始记录使用Spring Boot构建的非常简单的Web服务.

I'm pretty much new to Swagger and I started Documenting very simple my web service that I've build using Spring Boot.

问题是,在我配置了swagger之后,在浏览器中键入 localhost:8080/swagger-ui.html 后,出现以下屏幕,其中显示了一些奇怪的弹出消息,提示无法推断基本网址.当使用动态servlet注册或API位于API网关后面时,这很常见."
我确实知道这似乎是重复的问题,但是在给出所有这些答案的情况下,我根本无法解决此问题.接下来,我将屏幕截图和完整的代码发布到了没有出问题的地方.如果我做错了,请告诉我.

The problem is, After I configure swagger, in the browser when I type localhost:8080/swagger-ui.html I get this following screen with some weird popup message that says "Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway".
I do know it may seems repeated question, but I couldn't resolve this at all with all those answers given. Following, I've posted the screenshot and complete code where I didn't what went wrong. Please make me understand If I went wrong.

屏幕截图:

代码
SwaggerConfig.java

Code
SwaggerConfig.java

package com.test.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import static springfox.documentation.builders.PathSelectors.regex;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .paths(regex("/greet.*"))
                .build();
    }
}

TestApplication.java

TestApplication.java

package com.test.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages="com.test.controllers") 
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

TestController.java

TestController.java

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/greet")
public class TestController {

    @RequestMapping
        public String getGreeting() {
        return "Hello There";
    }
}

在上面的代码中, SwaggerConfig.java TestApplication.java 都属于同一包,即 com.test.config TestController.java 属于 com.test.controllers

In the above code both SwaggerConfig.java and TestApplication.java belongs to same package i.e com.test.config and TestController.java belongs to com.test.controllers

这是我拥有的所有代码,在pom.xml中,我具有以下两个依赖项

This is all the code I've and in the pom.xml I've two following dependencies

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.6.1</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.6.1</version>
        <scope>compile</scope>
    </dependency>

推荐答案

我解决了这个问题,在Application类上添加了注释@EnableSwagger2.

I solved it adding the annotation @EnableSwagger2 on Application class.

@SpringBootApplication
@EnableSwagger2 //this
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

这篇关于使用Spring Boot配置Swagger时获得意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆