处理 DefaultHandlerExceptionResolver :org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:意外字符 [英] Handle DefaultHandlerExceptionResolver : org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character

查看:146
本文介绍了处理 DefaultHandlerExceptionResolver :org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:意外字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误消息

WARN 13712 --- [nio-8080-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by handler execution: org.springframework.http.converter.HttpMessageNotReadableException: 
JSON parse error: Unexpected character (',' (code 44)): expected a value; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character (',' (code 44)): expected a value at [Source: java.io.PushbackInputStream@1db33478; line: 2, column: 16]

我正在使用这样的 spring 框架.

I'm using spring framework like this.

@GetMapping("/access")
public HashMap<String, Object> getAccess() {
    HashMap<String, Object> response = new HashMap<String, Object>();
    ArrayList<Access> list = new ArrayList<Access>();
    BdConector bd = BdConector.getInstance();
    list = bd.getAllAccess();

    if (list != null) {
        response.put("result", "200");
    } else {
        response.put("result", "500");
    }
    response.put("items", list);
    return response;
}

我想在发生解析器异常时记录 json body-content.

I would like to log the json body-content when a parser exception occurs.

推荐答案

试试这样...

@ExceptionHandler(com.fasterxml.jackson.core.JsonParseException.class)
        public ResponseEntity<Object> handleException(com.fasterxml.jackson.core.JsonParseException ex) {
            LOG.error("Error parse json " +  ex.getMessage());
            ...
        }

不太清楚如何提取无效 json 的原始字符串,也许它在异常的getMessage()"中,如我所示.无论如何,您可以调试ex"归档并找到如何正确获取它.

Not quite shure how to extract original string which is invalid json, maybe it is in exception's "getMessage()", as I shown. Anyway, you can debug "ex" filed and find how to properly get it.

这篇关于处理 DefaultHandlerExceptionResolver :org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:意外字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆