【问题标题】:Spring Boot swagger file without UI没有 UI 的 Spring Boot 招摇文件
【发布时间】:2019-02-25 18:16:47
【问题描述】:

我有一个在 Spring Boot 中构建的简单服务,它有一个简单的 API。我添加了 springfox 库以使用 swagger 和 swagger UI,但我不希望我的应用程序也为 UI 提供服务。我只想从/api/v1/api-docs得到定义

如何关闭 UI 部分?由于某种原因,不添加 swagger-ui 库作为依赖项不会删除 UI。

【问题讨论】:

    标签: spring spring-boot swagger


    【解决方案1】:

    您可以阻止 UI 并返回 HTTP 代码 404。类似于以下内容

    @Controller //note - this is a spring-boot controller, not @RestController
    public class HomeController {
        @RequestMapping ("/swagger/api/v1/api-docs")
        public String home(HttpServletRequest request) {
           throw new ResourceNotFoundException(); //Custom Solution
    
           or 
    
           throw new NoSuchRequestHandlingMethodException(request);
        }
    }
    
    
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    public class ResourceNotFoundException extends RuntimeException {
        ...
    }
    

    【讨论】:

      【解决方案2】:

      如果你使用的是 Spring Boot

      @SpringBootApplication
      public class MyApp {
          public static void main(String[] args) {
              SpringApplication.run(MyApp.class, args);
          }
      
          @Bean
          RouterFunction<ServerResponse> routerFunction() {
              return route(GET("/swagger"), req ->
                  ServerResponse.temporaryRedirect("<some 404 page>").build());
          }
      }
      

      【讨论】:

      • 这确实会修补端点,因此它不可用,但所有 UI 和控制器仍然在应用程序内注册,增加启动时间等。正在寻找一个完全删除它的解决方案。
      • 你能分享你的 pom 或 graddle 文件吗?
      • 这些是相关部分: implementation("io.springfox:springfox-swagger-ui") implementation("io.springfox:springfox-swagger2") implementation("org.springframework.boot:spring -boot-starter-web")
      • 如果去掉“io.springfox:springfox-swagger-ui”的引用会怎样
      猜你喜欢
      • 2017-10-06
      • 2015-12-03
      • 2018-09-11
      • 2022-01-12
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      相关资源
      最近更新 更多