在JSONException之后读取请求正文
我有一个Grails控制器,它接受带有JSON主体的POST请求。在一些请求中,JSON格式不正确,我得到一个JSONException。如何获取请求正文文本来记录或检查它(我并不总是能够访问客户端)?Grails在JSONException之后读取请求正文,json,grails,Json,Grails,我有一个Grails控制器,它接受带有JSON主体的POST请求。在一些请求中,JSON格式不正确,我得到一个JSONException。如何获取请求正文文本来记录或检查它(我并不总是能够访问客户端)?Grailsrequest.JSON已经使用了该流,因此看起来我无法通过request.body访问它 以下是我的控制器操作: def setScore(ScoreCommand scoreCommand) { try { def context = request.JSON.o
request.JSON
已经使用了该流,因此看起来我无法通过request.body访问它
以下是我的控制器操作:
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
}
这行吗
def json = request.JSON
try {
def context = json.optJSONObject("context")
userService.updateContext(context)
}catch(e) {
log.error json.toString()
}
它不起作用:(不幸的是,“上下文”在POST请求的主体中,而不是在参数中。
log.error(“错误解析JSON”,ce)
?哦,它在Isammoc中工作。就这么简单。如果你把它作为一个答案,我可以将它设置为接受。完整的消息包含JSONException和格式不正确的JSON。尝试了,没有。只要我访问'request.JSON',它就会尝试解析它并抛出异常。