我花了太多时间尝试调试以下问题,但我不确定问题出在哪里。
问题:得到400 Bad Request, Invalid Json
响应,但有以下例外:
com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: [B@6ee503c9; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164) ~[jackson-databind.jar:2.2.2]
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:2931) ~[jackson-databind.jar:2.2.2]
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:2846) ~[jackson-databind.jar:2.2.2]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1569) ~[jackson-databind.jar:2.2.2]
at play.api.libs.json.JacksonJson$.parseJsValue(JsValue.scala:480) ~[play-json_2.10.jar:2.2.0]
at play.api.libs.json.Json$.parse(Json.scala:27) ~[play-json_2.10.jar:2.2.0]
在我的控制器中测试的方法:
def createArticle(id: String) =
Action.async(parse.json) { implicit request =>
(request.body \ "content").asOpt[String].map {
............
............
}.getOrElse(BadRequest("Invalid request body"))
}
对应的单元测试:
"create article" in {
running(FakeApplication()) {
val postJson = Json.obj("content" -> "article content")
val result = resource.createArticle(ARTICE_ID)(FakeRequest(POST, controllers.routes.ArticleResource.create(ARTICLE_ID).url).withJsonBody(postJson).withHeaders(CONTENT_TYPE -> "application/json").run
status(result) must equalTo OK
}
}
我在这里阅读了讨论,但那里的建议都没有帮助。