在使用Spring Boot配置Swagger时出现意外结果

8

我对Swagger不是很熟悉,最近开始使用Spring Boot构建Web服务的文档。

问题在于,在我配置Swagger之后,当我在浏览器中键入localhost:8080/swagger-ui.html时,我得到了以下带有一些奇怪弹出消息的屏幕,上面写着“无法推断基本URL。当使用动态servlet注册或API位于API网关后面时,这是常见的。”
我知道这可能是重复的问题,但我根本无法通过所有给出的答案解决它。接下来,我发布了截图和完整代码,我不知道哪里出了问题,请帮忙理解。

截图:进入图像说明

代码
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

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

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.config,而 TestController.java 属于 com.test.controllers

这是我所有的代码,在 pom.xml 中有以下两个依赖项。
<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>

对我来说它有效。有时候当你打开一些浏览器扩展时,这些问题会出现。尝试使用隐身窗口或其他浏览器。 - Barath
在某些浏览器中,比如Mozilla,可能没有弹出警示窗口,但是文档仍然完全没有出现。在TestController中,对于getGreeting()方法,应该有一个可见的文档,不是吗? - Kishore Kumar Korada
@Barath 你确定你能看到文档吗? - Kishore Kumar Korada
是的,我能够通过您的配置看到Swagger文档。我之前也遇到过类似的问题,那是由于浏览器缓存的原因,请尝试清除缓存。 - Barath
请仔细检查您的@ComponentScan,看起来您的SwaggerConfig根本没有被扫描。添加sysout并检查一下。因为com.test.controllers正在被扫描,而com.test.config位于不同的包中。 - Barath
6个回答

6

我在Application类上添加了注解@EnableSwagger2,这样就解决了问题。

@SpringBootApplication
@EnableSwagger2 //this
public class Application {

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

}

1
谢谢!它可以工作了。我在部署到AWS EC2时也遇到了同样的问题。 - deviant

6

当我将Spring-boot应用程序部署为war包时,遇到了同样的问题。在使用嵌入式Tomcat(作为独立的Spring-boot应用程序)运行应用程序时,它可以正常工作,但是在将war文件部署到远程Tomcat时,会出现上述问题。

经过一番搜索,我发现我的应用程序缺少war归档文件的Servlet Initializer。

以下内容对我非常有效。

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application){ 
   return application.sources(TestApplication.class); 
   }
}

1
如果Swagger位于任何验证之后,您需要在SpringBoot Security中执行以下操作。
http.authorizeRequests().antMatchers("/swagger-resources/**").permitAll().anyRequest().fullyAuthenticated();

然后您访问{{link1:Swagger UI文档}}


1
只需在ResourceServerConfiguration类的configure方法中添加以下三个URL:"/swagger-ui","/swagger-ui.html" 和 "/swagger-resources/**"。这样可以解决你的问题。

0
在我的情况下,这个bean配置是一个问题制造者。
@Bean
public CsvMapper csvSerializerMapper() {
    stuff()
}

Spring使用它来初始化Spring的Jackson ObjectMapper,但是它默默地失败了。 通过添加具有对象映射器的bean来修复它。

@Autowired
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
    return builder.createXmlMapper(false).build();
}

0

嗯,尝试将.paths更改为.apis(RequestHandlerSelectors.basePackage("你的控制器所在的包名com.demo.example.controller")).build(); 试试这个方法,如果不行请告诉我。


不,我无法在浏览器中查看文档。在此URL“http://localhost:8080/swagger-ui.html”上,它只显示弹出窗口,就像我在问题中指定的屏幕截图一样。 - Kishore Kumar Korada
抱歉,您是想通过 localhost:8080/greet/swagger-ui.html 或者仅通过 localhost:8080/swagger-ui.html 访问文档吗? - SkiiNet
尝试访问greet one,我认为那应该是你的服务的实际路径。 - SkiiNet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接