如何为 springfox 修改 swagger-ui.html?

How can I modify swagger-ui.html for springfox?

我正在使用 springfox 的 swagger 实现。我想修改 swagger-ui.html 以采用自定义 header 值。我该如何修改这个文件?或者告诉 spring fox 使用备用文件?

如果您要稍微自定义 swagger-ui quite,我建议您将 swagger-ui 的私有副本添加到您的 Web 应用程序资源中并根据您的需要修改 ui。

Swagger-ui 与 springfox 捆绑在一起 可选

是的,将 swagger ui 拉入您的项目并按照此处的步骤操作: https://github.com/springfox/springfox/issues/1176

然后编辑 index.html 并将其指向宠物店的位置更改为您的端点。 (即“http://localhost:8080/MyProjectName/v2/api-docs”);

 $(function () {
  var url = window.location.search.match(/url=([^&]+)/);
  if (url && url.length > 1) {
    url = decodeURIComponent(url[1]);
  } else {
    url = "http://petstore.swagger.io/v2/swagger.json";
  1. 从 github.

    拉 Swagger UI

    https://github.com/swagger-api/swagger-ui

  2. 将 swagger ui 的 dist 目录复制并粘贴到 resources 目录

  3. 在 swagger 配置中用下面的代码替换

    替换

    registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
    

    registry.addResourceHandler("**").addResourceLocations("classpath:/dist/");
    
  4. 将 index.html 中的 api 文档路径更改为您的路径

    替换

    http://petstore.swagger.io/v2/swagger.json
    

    http://localhost:8080/your_app/v2/api-docs
    
  5. 使用下面的 url 模式查看 ui 页面

    http://localhost:8080/your_app/index.html