2013-01-21 5 views
1

JSON 본문에 POST 요청을 받아들이는 Grails 컨트롤러가 있습니다. 일부 요청에서는 JSON이 제대로 형성되지 않았고 JSONException이 발생합니다. 요청 본문 텍스트를 기록하거나 검사 (클라이언트에 항상 액세스 할 수있는 것은 아님)하려면 어떻게해야합니까? Grails request.JSON은 이미 스트림을 소비 했으므로 request.body를 통해 액세스 할 수없는 것처럼 보입니다.JSONException 다음에 요청 본문을 읽습니다.

def setScore(ScoreCommand scoreCommand) { 
    try { 
     def context = request.JSON.optJSONObject("context") 
     userService.updateContext(context) 
    } catch (ConverterException ce) { 
     log.error("Error parsing JSON") 
     // I want to log the malformed JSON body 
    } 
    def scoreResponse 
    if (!scoreCmd.validate()) { 
     scoreResponse = errorResponse(scoreCommand) 
    } else { 
     def scoreResult = gameService.setScore(scoreCommand) 
     scoreResponse = buildResponse(scoreResult) 
    } 
    render scoreResponse as JSON 
} 
+0

'params.context'? –

+0

작동하지 않습니다 :(불행히도 "context"는 POST 요청 본문에 있고 매개 변수가 아닙니다.) – chozero

+1

'log.error ("JSON 구문 분석 오류", ce)? – Isammoc

답변

0

이 일을합니까 : 여기

내 컨트롤러 액션인가?

def json = request.JSON 

try { 
    def context = json.optJSONObject("context") 
    userService.updateContext(context) 
}catch(e) { 
    log.error json.toString() 
} 
+0

시도했으나 아니요. 'request.JSON'에 액세스하면 바로 시도합니다. 그것을 파싱하고 예외를 throw합니다. – chozero

+0

아, 그게 오류가 던져지고 있는지 궁금해. – Gregg

관련 문제