簡體   English   中英

處理 DefaultHandlerExceptionResolver :org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析錯誤:意外字符

[英]Handle DefaultHandlerExceptionResolver : org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character

我收到以下錯誤消息

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 框架。

@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。

嘗試這樣的事情......

@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”歸檔並找到如何正確獲取它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM