aşağıdaki eylemiPlay Framework 2: organı olarak bir json dize ile bir istek sınamak
def save() = Action(parse.json) { implicit request =>
request.body.asOpt[IdeaType].map { ideatype =>
ideatype.save.fold(
errors => JsonBadRequest(errors),
ideatype => Ok(toJson(ideatype))
)
}.getOrElse (JsonBadRequest("Invalid type of idea entity"))
}
var Ve web hizmeti böyle Kıvrık Tamam çalışır onu
test etmek isterim: doğru yeni bir kaynak döner
curl -X post "http://localhost:9000/api/types"
--data "{\"name\": \"new name\", \"description\": \"new description\"}"
--header "Content-type: application/json"
{"url":"/api/types/9","id":9,"name":"new name","description":"new description"}
şey eksik muyum:
[error] ! add a new ideaType, using route POST /api/types
[error] ClassCastException: java.lang.String cannot be cast to play.api.libs.json.JsValue (IdeaTypes.bak.scala:35)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:36)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:35)
[error] play.api.mvc.Action$$anon$1.apply(Action.scala:170)
Bu soru üzerine tavsiyelerde izledi:
Ben
"add a new ideaType, using route POST /api/types" in {
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
val json = """{"name": "new name", "description": "new description"}"""
val Some(result) = routeAndCall(
FakeRequest(
POST,
"/api/types",
FakeHeaders(Map("Content-Type" -> Seq("application/json"))),
json
)
)
status(result) must equalTo(OK)
contentType(result) must beSome("application/json")
val Some(ideaType) = parse(contentAsString(result)).asOpt[IdeaType]
ideaType.name mustEqual "new name"
}
}
ile test çalışıyorum Ama şu hatayı alıyorum ?
-
Kim Stebel çözümü iyi çalıştı, ama sonra bu gibi withJsonBody ile çalıştı:
val jsonString = """{"name": "new name", "description": "new description"}"""
val json: JsValue = parse(jsonString)
val Some(result) = routeAndCall(
FakeRequest(POST, "/api/types").
withJsonBody(json)
)
ve aşağıdaki hatayı alıyorum:
[error] ! add a new ideaType, using route POST /api/types
[error] ClassCastException: play.api.mvc.AnyContentAsJson cannot be cast to play.api.libs.json.JsValue (IdeaTypes.bak.scala:35)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:36)
[error] controllers.IdeaTypes$$anonfun$save$1.apply(IdeaTypes.bak.scala:35)
bir fikrin?
hattı
değiştirmek? –
istisnası oluşur "def save() = Eylem ..." satır ... – opensas
rotanızı yapıştırınAndCall yardımcı olabilir. – wliao