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

查看:39
本文介绍了使用 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 时,我会看到以下屏幕,并带有一些奇怪的弹出消息,上面写着无法推断"基本 url.这在使用动态 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.javaTestApplication.java 属于同一个包,即 com.test.configTestController.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天全站免登陆