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

Play Framework 2.5.x集成Swagger UI报错求助

Hey there! Let’s troubleshoot your Swagger integration issue with Play Framework 2.5.x. Since you’ve already added dependencies and configured routes but hit errors accessing swagger.json, here are the key steps to check and missing configurations you might need:

1. Verify Swagger Dependency Compatibility

Play 2.5.x is an older release, so you need to use a swagger-play2 version explicitly built for it—typically the 1.5.x series. Double-check your build.sbt for the correct dependency (match it to your project’s Scala version, e.g., 2.11 or 2.12):

libraryDependencies += "io.swagger" %% "swagger-play2" % "1.5.18"

Avoid newer 2.x+ versions of swagger-play2, as they’re designed for newer Play releases and will cause compatibility breaks.

2. Enable Swagger Module in Application Config

Play 2.5.x uses Guice for dependency injection, so you must enable the Swagger module in conf/application.conf:

play.modules.enabled += "play.modules.swagger.SwaggerModule"

Also add basic Swagger metadata here to prevent generation errors:

swagger.api.basepath = "http://localhost:9000" # Match your app's base URL
swagger.api.info.title = "Your API Name"
swagger.api.info.description = "Documentation for your service's endpoints"
swagger.api.info.version = "1.0"
3. Add Required Swagger Annotations to Your APIs

Swagger generates swagger.json by scanning annotations on your controllers and models—missing these will cause the spec generation to fail:

  • On your controller class (Java example):
    @Api(value = "/users", description = "Operations for managing user accounts")
    public class UserController extends Controller {
        // Controller logic
    }
    
    (For Scala controllers, use the Scala-compatible annotations from io.swagger.annotations)
  • On individual API methods:
    @ApiOperation(
        value = "Fetch a user by ID",
        notes = "Returns a full user object if the ID exists in the system",
        response = User.class
    )
    public Result getUser(Long id) {
        // Method logic
    }
    
  • On model classes (e.g., User):
    @ApiModel(value = "User", description = "Details of a user account")
    public class User {
        @ApiModelProperty(value = "Unique user identifier", required = true)
        private Long id;
        // Getters and setters
    }
    

Without these annotations, Swagger has no data to build the JSON spec.

4. Validate Route Configuration

Double-check your conf/routes entries to ensure they point to the correct Swagger-provided controllers:

# Serve the Swagger JSON specification
GET     /swagger.json           controllers.ApiHelpController.getResources
# Serve Swagger UI static files
GET     /swagger-ui/*file       controllers.Assets.at(path="/public/lib/swagger-ui", file)

Note: ApiHelpController is part of the swagger-play2 library—don’t try to implement it yourself.

5. Check Logs for Exact Error Details

When you hit an error accessing swagger.json, look at Play’s console logs or logs/application.log—the error message will pinpoint the issue:

  • Guice injection errors mean the Swagger module isn’t enabled properly
  • Annotation-related errors indicate a misconfigured controller/model annotation (e.g., missing required parameters)
  • "Class not found" errors signal a mismatch between your dependency version and Play/Scala version
6. Ensure Swagger UI Static Assets Are Accessible

If swagger.json starts working but the Swagger UI page (/swagger-ui/index.html) returns a 404:

  • For swagger-play2 1.5.x, you may need to manually add Swagger UI 2.x static files to your project’s public/lib/swagger-ui directory. Download the appropriate version, unzip it, and copy the files to this path.
7. Restart Your Play Application

Sometimes in Play’s dev mode, hot reloading skips annotation scanning. Try stopping and restarting the app to ensure all Swagger components load correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:09:38