5

我有一个播放框架 2.1.1 (Java) 的问题,告诉我我正在发送无效的 JSON。播放框架 2.x 和 2.1.0 存在类似/可能相同的问题,但它应该在播放框架 2.1.1 afaik 中解决:请参阅Play Framework 2.1 中的无效 JSON

这就是我在代码中所做的,我试着让它更短一点:

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import play.libs.Json;
import play.mvc.BodyParser;

@BodyParser.Of(BodyParser.Json.class)
public static Result login() {
    JsonNode json = request().body().asJson();
}

当我运行时:

curl -v -H "Content-Type: application/json" -X POST -d '{"email":"test@test.de","password":"test"}' http://localhost:9000/mobile/login

我得到以下回复:

< HTTP/1.1 400 Bad Request
< Content-Type: text/html; charset=utf-8
< Content-Length: 1917
...
<p id="detail">
    For request 'POST /mobile/login' [Invalid Json]
</p>
...

我已经清理了我的项目并重新运行了几次。运行play about时,我得到以下输出:

[info] Loading project definition from /path/to/project
[info] Set current project to FFPushAlarmPlay (in build file:/path/to)
[info] This is sbt 0.12.2
[info] The current project is {file:/path/to}...
[info] The current project is built against Scala 2.10.0
[info] Available Plugins: play.Project, sbt.PlayProject, com.typesafe.sbteclipse.plugin.EclipsePlugin, com.typesafe.sbtidea.SbtIdeaPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.9.2

我做错了什么吗?任何帮助将不胜感激,因为这是我的第一个 Play 2 项目!

更新:我忘了提到我还有一个 iOS 客户端,它使用 AFNetworking 自动构建 JSON,我也在那里得到一个无效的 JSON 响应。所以它似乎不是无效的 JSON 导致这个......

4

3 回答 3

14

您的代码没有任何问题。你是用windows命令行(CMD.exe)运行的curl吗?如果您使用CMD.exeto run curl,则必须修复要传递的 JSON 格式数据。我认为在 Windows 中,使用引号与 UNIX 机器有点不同。

最简单的解决方法是避免使用单引号并使用双引号来开始和结束 JSON 数据,将 JSON 数据中的双引号转义(按\字符),可能有点累,但它应该可以工作:

curl -v -H "Content-Type: application/json" -X POST -d "{\"email\":\"test@test.de\",\"password\":\"test\"}" http://localhost:9000/mobile/login

或者,您可以使用cygwin其他类似 UNIX 的Windows 命令行作为替代命令行,因为它的行为可能与 UNIX 机器一样。

希望对你朋友有用。:)

于 2013-05-13T02:31:40.000 回答
0

Alright, I finally got back to looking into this and I found the answer. The problem for me was that there were people with similar issues in Play <2.1.1 and my belief was this was a similar issue, which it wasn't.

I added an unmanaged dependency to the project in the lib directory of my play-project. The library was notnoop java-apns (https://github.com/notnoop/java-apns). Internally, it uses the org.codehaus.jackson-classes which the Play framework also uses. I used version 0.1.5 which is fairly old.

The fix, just remove the apns-library from the lib-directory, remove it from the build-path in eclipse and then in project/target/Build.scala add a managed dependency:

"com.notnoop.apns" % "apns" % "0.2.3"

Hope this helps whoever runs into similar issues!

于 2013-07-14T21:33:29.257 回答
-1

我在 CMD 中遇到了同样的问题,这解决了问题:

C:\Windows\System32>curl -v -u login:password -X PUT -d "{\"test\":\"aaa\",\"name\":\"Kowalski\"}" http://127.0.0.1:5984/albums/2340234802938424f7w783

PS 在程序 Cygwin 中不会出现此问题。

于 2015-05-14T19:59:50.960 回答