Springfox - 改变 swagger-ui 的路径 [英] Springfox - change path of swagger-ui

查看:60
本文介绍了Springfox - 改变 swagger-ui 的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在从 http://localhost:8080/myContextRoot/swagger-ui 提供 swagger.html

我可以配置 swagger 及其资源在 http://localhost:8080/myContextRoot/swagger/swagger-ui.html

Can I configure swagger and its resources to be served under http://localhost:8080/myContextRoot/swagger/swagger-ui.html

感谢您的帮助!

推荐答案

我遵循了这个方法:

依赖:

implementation 'org.tuckey:urlrewritefilter:4.0.4'

UrlRewrite 类:

UrlRewrite Class:

@Component
public class SwaggerUrlRewriteFilter extends UrlRewriteFilter {
  private static final String CONFIG_LOCATION = "/urlrewrite.xml";

  @Value("classpath:/urlrewrite.xml")
  private Resource resource;

  @Override
  protected void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
    try {
      //Create a UrlRewrite Conf object with the injected resource
      Conf conf = new Conf(filterConfig.getServletContext(), resource.getInputStream(), resource.getFilename(), "@@yourOwnSystemId@@");
      checkConf(conf);
    } catch (IOException ex) {
      throw new ServletException("Unable to load URL rewrite configuration file from " + CONFIG_LOCATION, ex);
    }
  }
}

在资源文件夹 urlrewrite.xml

and in the resource folder urlrewrite.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
        "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite>
    <rule>
        <from>/swagger/swagger-ui.html</from>
        <to type="passthrough">/swagger-ui.html</to>
    </rule>

    <rule>
        <from>/swagger/webjars/(.*)</from>
        <to type="passthrough">/webjars/$1</to>
    </rule>

    <rule>
        <from>/swagger/v2/api-docs</from>
        <to type="passthrough">/v2/api-docs</to>
    </rule>

    <rule>
        <from>/swagger/configuration/(.*)</from>
        <to type="passthrough">/configuration/$1</to>
    </rule>

    <rule>
        <from>/swagger/swagger-resources</from>
        <to type="passthrough">/swagger-resources</to>
    </rule>
</urlrewrite>

之后大摇大摆的路径是:

after that the path to swagger is:

http://localhost:8080/myContextRoot/swagger/swagger-ui.html

这篇关于Springfox - 改变 swagger-ui 的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆