【问题标题】:Need to update swagger UI Path for spring boot project需要为 spring boot 项目更新 swagger UI Path
【发布时间】:2020-11-11 14:40:17
【问题描述】:

我有一个 Spring Boot 项目,需要从

更新我的招摇路径
http://localhost:9012/swagger-ui.html to http://localhost:9012/myservice/swagger-ui.html

我没有为我的应用程序使用任何上下文路径我已经尝试了以下所有方法,但都没有奏效。

Change location to call swagger-ui in Spring

https://github.com/springfox/springfox/issues/1443

How to change swagger-ui.html default path

https://github.com/springfox/springfox/issues/1080

How to change Swagger-ui URL prefix?

private static final String PATH = "/myservice";

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    final String apiDocs = "/v2/api-docs";
    final String configUi = "/swagger-resources/configuration/ui";
    final String configSecurity = "/swagger-resources/configuration/security";
    final String resources = "/swagger-resources";
    final String swaggerUI = "/swagger-ui.html";

    registry.addRedirectViewController(apiDocs, PATH +apiDocs).setKeepQueryParams(true);
    registry.addRedirectViewController(resources, PATH +resources);
    registry.addRedirectViewController(configUi, PATH +configUi);
    registry.addRedirectViewController(configSecurity, PATH +configSecurity);
    registry.addRedirectViewController(swaggerUI, PATH + swaggerUI);
    registry.addRedirectViewController("/",PATH);
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry){
    registry.addResourceHandler(PATH + "/swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
    registry.addResourceHandler(PATH + "/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  }

我正在使用 swagger 版本 2.9.2 有没有其他方法可以解决这个问题。

【问题讨论】:

  • 尝试在您的 application.properties 中添加上下文路径server.servlet.contextPath=/myservice 看看是否有效。
  • 嗨 LUC1F3R 和 Sudoss,如果我向我的应用程序添加上下文路径,这两种方式都建议添加上下文路径,所有其他 API URL 也会发生我不想要的更改。
  • @Kashyap 知道了...看起来重定向/转发就是您要找的东西。请尝试使用代码 sn-p here 并告诉我们这是否适合您。

标签: java spring-boot swagger swagger-ui swagger-2.0


【解决方案1】:

在你的控制器中试试这个重定向方法,我已经测试过了,它可以工作

@RequestMapping("/myservice/swagger-ui.html")
public String redirectToSwagger() {
    return "redirect:/swagger-ui.html";
}

您可以根据需要使用重定向或转发。

【讨论】:

    【解决方案2】:

    您可以尝试下面的代码,它需要@Controller 而不是@RestController

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import springfox.documentation.annotations.ApiIgnore;
    
    @Controller
    @ApiIgnore
    public class SwaggerMappingController {
    
        @RequestMapping("/swagger-ui.html")
        public String greeting() {
            return "redirect:/swagger-ui/index.html";
        }
    }
    

    【讨论】:

      【解决方案3】:

      请使用以下 swagger 依赖项。您所要做的就是添加 pom.xml,它会像魅力一样工作。删除任何与 swagger 相关的注释,例如 @EnableSwagger2

      <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-boot-starter</artifactId>
          <version>3.0.0</version>
      </dependency>
      

      Swagger 网址:http://{base_url}/swagger-ui/index.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-04
        • 2016-04-13
        • 2018-01-27
        • 1970-01-01
        • 2021-01-03
        • 1970-01-01
        • 2017-11-17
        • 2021-09-23
        相关资源
        最近更新 更多