zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【异常】SpringMVC请求异常JSON parse error: Unexpected character (‘}‘ (code 125)): was expecting double-quote

SpringMVC异常codeJSONJSON Error 请求 was
2023-09-14 09:04:55 时间

一、报错内容

2023-03-07 14:42:55.866 [apb-cloud-apb-terminal-api] [XNIO-1 task-1] ERROR 
GlobalExceptionHandler - 请求地址'/api/V1/card/fetchLatestLauncherCardByCardKey',发生未知异常.
org.springframework.http.converter.HttpMessageNotReadableException: 
JSON parse error: Unexpected character ('}' (code 125)): 
was expecting double-quote to start field name; 
nested exception is com.fasterxml.jackson.core.JsonParseException: 
Unexpected character ('}' (code 125)): was expecting double-quote to start field name
Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('}' (code 125)): 
was expecting double-quote to start field name

返回值如下

{
    "code": 500,
    "message": "JSON parse error: Unexpected character ('}' (code 125)): was expecting double-quote to start field name; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('}' (code 125)): was expecting double-quote to start field name\n at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 3, column: 2]",
    "data": null
}

二、报错说明

2.1 Controller层的代码如下

    @PostMapping("/fetchLatestLauncherCardByCardKey")
    public R<LauncherCardVehicleResp> fetchLatestLauncherCardByCardKey(@RequestBody @Valid LauncherCardVehicleReq req) {
        return launcherCardService.fetchLatestLauncherCardByCardKey(req.getCardKey());
    }

2.2 实体的代码如下

@Setter
@Getter
public class LauncherCardVehicleReq implements Serializable {
  private static final long serialVersionUID = 1L;
  private String cardKey;
}

2.3 接口请求如下

在这里插入图片描述

三、问题解决

好吧,ApiFox调用的时候,请求体末尾加上了逗号,这导致它不是一个JSON格式。将请求体末尾的逗号删除,重新调用即可。