首页 文章

使用Spring Boot配置Swagger时获得意外结果

提问于
浏览
7

我是Swagger的新手,我开始使用Spring Boot构建非常简单的Web服务 .

问题是,在我配置swagger之后,当我输入 localhost:8080/swagger-ui.html 时,我在浏览器中看到了以下屏幕,其中包含一些显示"Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway"的奇怪弹出消息 .
我知道这似乎是重复的问题,但我无法发布屏幕截图和完整的代码,我没有出错 . 如果我出错了,请让我明白 .

截屏:
enter image description here

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

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.configTestController.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>

5 回答

  • 0

    在将Spring-boot应用程序部署为war工件时,我遇到了同样的问题 . 在使用嵌入式tomcat运行应用程序时(作为独立的spring-boot应用程序),在将war文件部署到面向上述问题的远程tomcat的同时,它正常工作 .

    在挖掘中,我发现我错过了我的战争档案应用程序的Servlet初始化程序 .

    以下为我工作就像一个魅力 .

    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); 
       }
    }
    
  • 5

    我解决了它在Application类上添加注释 @EnableSwagger2 .

    @SpringBootApplication
    @EnableSwagger2 //this
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
    }
    
  • 5

    嗯,尝试将.paths更改为.apis(RequestHandlerSelectors.basePackage(“你的控制器是com.demo.example.controller的包”)) . build();试一试,如果它不起作用,请告诉我 .

  • 0

    如果Swagger支持任何身份验证,则需要在SpringBoot Security中执行以下操作

    http.authorizeRequests().antMatchers("/swagger-resources/**").permitAll().anyRequest().fullyAuthenticated();
    

    然后你访问Swagger UI Documentation

  • 0

    在我的情况下,这个bean配置是一个问题制造者 .

    @Bean
    public CsvMapper csvSerializerMapper() {
        stuff()
    }
    

    Spring使用它来初始化Spring的jackson objectMapper,它默默地失败了 . 通过添加带对象映射器的bean来修复它 .

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

相关问题