You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Play3/Scala2.13集成Swagger失败,求助解决方案

Play3/Scala2.13集成Swagger问题解决方案

问题1:执行play-swagger官方步骤到第三步时,报scala.ScalaReflectionException: class Asset not found

  • 确认play-swagger依赖版本适配Play3:Play3对应的play-swagger最新稳定版为0.19.x,build.sbt中依赖配置如下:
    libraryDependencies += "com.iheart" %% "play-swagger" % "0.19.0" % Compile
    
  • 检查Assets类导入路径:Play3中Assets控制器位于play.api.mvc.Assets,若代码中引用旧路径(如play.mvc.Assets)会导致类找不到,修正导入语句为import play.api.mvc.Assets
  • 清理编译缓存:执行sbt clean compile,避免缓存残留引发的类查找异常

问题2:swagger.json数据正确,但Swagger UI仍加载官方Petstore API

  • 修改Swagger UI初始化配置:找到assets/lib/swagger-ui/swagger-initializer.js,直接指定本地swagger.json路径:
    window.onload = function() {
      const ui = SwaggerUIBundle({
        url: "/swagger.json",
        dom_id: '#swagger-ui',
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIStandalonePreset
        ],
        layout: "StandaloneLayout"
      });
      window.ui = ui;
    };
    
  • 验证重定向参数:若坚持用重定向方式,检查浏览器地址栏是否存在?url=/swagger.json参数,若存在仍加载默认API,建议升级Swagger UI到4.x系列版本
  • 检查CORS配置:如果/swagger.json接口存在跨域问题,浏览器会 fallback 到默认API,在application.conf中添加CORS配置:
    play.filters.cors {
      allowedOrigins = ["*"]
      allowedHttpMethods = ["GET", "POST", "PUT", "DELETE"]
      allowedHttpHeaders = ["Accept", "Content-Type"]
    }
    

内容的提问来源于stack exchange,提问作者Janani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 19:54:54