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

时间:2016-03-22 21:19:36

标签: springfox

我正在使用springfox的swagger实现。我想修改swagger-ui.html以获取自定义标头值。如何修改此文件?或者告诉spring fox使用备用文件?

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

  1. 从github拉出Swagger UI。

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

  2. 复制&将swagger ui的 dist 目录粘贴到资源目录

  3. 在swagger config

    中替换下面的代码

    替换

    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. 使用以下网址格式查看ui页面

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

答案 2 :(得分:1)

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

然后编辑index.html并将其指向petstore的位置更改为您的端点。 (即" 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";