Play Framework 2.6.6 URI长度超限及JSON解析异常求助
Hey there, let's work through the two issues you're hitting with Play Framework 2.6.6 one by one:
Play Framework's default Netty server sets a 2048-character limit on the initial request line (which includes the URI). To lift this restriction, you'll need to update your application.conf file:
- Add the following configuration to increase the limit (adjust the value to suit your needs—8192 is a common choice, but you can go higher if necessary):
play.server.netty.maxInitialLineLength = 8192
If you're using a reverse proxy like Nginx in front of your Play app, don't forget to update its corresponding settings too. For Nginx, tweak large_client_header_buffers to match or exceed the limit you set in Play, otherwise the proxy itself will block long URIs before they reach your app.
That JsonParseException is a secondary issue: when Play hits the URI length limit, it returns a plain-text error response by default, but your code is still trying to parse that response as JSON. Here's how to fix this:
- First, address the root cause: Make sure you've applied the URI limit fix above—this will prevent the error from being triggered in the first place for valid requests.
- Switch to POST for large data: If you're passing a lot of data via GET parameters, consider using a POST request instead. GET URIs are inherently limited by browsers and servers, whereas POST uses the request body which has no strict length restrictions.
- Add error handling in client code: If you're calling this API from a frontend or another service, check the HTTP status code first. If you get a 414 (URI Too Long) or any other 4xx/5xx error, don't attempt to parse the response body as JSON—handle the error message directly.
- Customize Play's error handler: For a more robust fix, create a custom error handler that returns properly formatted JSON errors instead of plain text. This way, even if an error occurs, your JSON parser won't choke on invalid syntax.
Here's a quick example of a custom error handler in Scala:
import play.api.http.HttpErrorHandler import play.api.mvc._ import play.api.mvc.Results._ import play.api.libs.json.Json import scala.concurrent._ class CustomJsonErrorHandler extends HttpErrorHandler { override def onClientError(request: RequestHeader, statusCode: Int, message: String): Future[Result] = { Future.successful { statusCode match { case 414 => BadRequest(Json.obj("error" -> "URI length exceeds the configured limit")) case _ => Status(statusCode)(Json.obj("error" -> message)) } } } override def onServerError(request: RequestHeader, exception: Throwable): Future[Result] = { Future.successful { InternalServerError(Json.obj("error" -> "An unexpected server error occurred")) } } }
Then register this handler in application.conf:
play.http.errorHandler = "com.your.package.CustomJsonErrorHandler"
By combining these steps, you'll both fix the URI length restriction and eliminate the subsequent JSON parsing failure.
内容的提问来源于stack exchange,提问作者user7200354

