2016-06-22 3 views
0

내 질문은 어떻게 applyunapply하지만 스칼라 할 수있는 방법이 없습니다 세 번째 부분으로 자바 class에 JSON을 변환 할 수 있음이다, 매우 간단합니다 case class. 자세한 코드는 사전에이 다음, 자바 객체에 JsValue을 구문 분석

def login = Action { implicit request => 
    val jsonBody = request.body.asJson // the jsonBody is value of Option[JsValue] 

    val myClass = //some method can be provided to convert the jsonBody to myClass(a third-part java class) 
} 

감사처럼

당신은 MyCaseClass 같은 경우 클래스를 정의 할 수 있습니다

답변

0

(A : 문자열, B : int)를, 당신은 다음과 같이 얻을 JSON 확인 :

request.body.asJson.validate[MyCaseClass] match { 
    case myCaseClass: JsSuccess[MyCaseClass] => new JavaClass(a, b) 
    case error: JsError => //handle the error 
} 

유효한 Json 개체가 있는지 확인하려면 Action(parse.json) { implicit request =>을 사용할 수 있습니다.

+0

귀하의 빠른 답변 주셔서 감사합니다,하지만 그것은 내 원하는 아니에요. 또 다른 한가지로,'parse.json'은 content-type이'application/json' 일 때만 사용될 수 있습니다. content-type이'application/json '이 아닌 경우'parse.json'을 사용하면 요청 본문은 None으로 파싱됩니다. – Jerry

+0

content-type이 application/json이 아닌 경우 .asJson을 수행 할 때 오류가 발생합니다. 그리고 그것이 당신이 원하는 것이 아니라면, 나는 그들을 얻지 못하기 때문에 자세히 말하십시오. – Simon