【问题标题】:Springfox - change path of swagger-uiSpringfox - 改变swagger-ui的路径
【发布时间】:2020-05-12 18:04:33
【问题描述】:

现在 swagger 由 http://localhost:8080/myContextRoot/swagger-ui.html 提供服务

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

感谢您的帮助!

【问题讨论】:

标签: spring-boot swagger-ui springfox


【解决方案1】:

我采用了这种方法:

依赖:

implementation 'org.tuckey:urlrewritefilter:4.0.4'

UrlRewrite 类:

@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

<?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>

之后招摇的路径是:

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

【讨论】:

    猜你喜欢
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    相关资源
    最近更新 更多