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
}
'params.context'? –
작동하지 않습니다 :(불행히도 "context"는 POST 요청 본문에 있고 매개 변수가 아닙니다.) – chozero
'log.error ("JSON 구문 분석 오류", ce)? – Isammoc