nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘xxx‘错误的详细解决方法

03-22 1237阅读 0评论

文章目录

  • 1.复现错误
  • 2. 分析错误
  • 3. 解决错误
  • 4. 文末总结

    1.复现错误

    nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘xxx‘错误的详细解决方法,nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘xxx‘错误的详细解决方法,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,ChatGPT,方法,第1张
    (图片来源网络,侵删)

    今天写好导入hive表的接口,如下代码所示:

    /**
     * hive表导入
     *
     * @author super先生
     * @datetime 2023/3/20:16:32
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/xxx/importTables")
    public ServiceStatusData localHiveImportTables(
        @RequestBody ImportTablesBo importTablesBo, 
        @RequestHeader("x-userid") Long userId) {
        
      logger.info("入参记录:importTablesBo={},userId={}", importTablesBo, userId);
      if (isBlank(importTablesBo.getHiveTableName())) {
        return new ServiceStatusData(ServiceStatusData.Status.Fail, "hive表名不能为空", null);
      }
      if (isBlank(importTablesBo.getTableImportType())) {
        return new ServiceStatusData(ServiceStatusData.Status.Fail, "导表类型不能为空", null);
      }
      if (isBlank(importTablesBo.getCron())) {
        return new ServiceStatusData(ServiceStatusData.Status.Fail, "执行周期表达式不能为空", null);
      }
      if (null == importTablesBo.getDatasetId()) {
        return new ServiceStatusData(ServiceStatusData.Status.Fail, "工作表id不能为空", null);
      }
      // TODO 调用service层的方法
      ......
      return new ServiceStatusData(ServiceStatusData.Status.Success, "", importTablesBo);
    }
    

    同时,使用Ajax调用导入hive表的接口,如下代码所示:

    
    
        
        content-type为application/json
        
        
            function importTables() {
                const importTablesReq = {
                    "hiveTableName": "project",
                    "tableImportType": "1",
                    "pkColumn": "id",
                    "incrementColumn": "projectname",
                    "cron": "0 0 11 * * ?",
                    "datasetId": 2
                };
                $.ajax({
                    url: 'http://localhost:8080/.../importTables.do',
                    type: 'post',
                    headers: {
                        "x-userid": 1
                    },
                    data: importTablesReq,
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        console.log("data=", data)
                    }
                });
            }
        
    
    
    点击按钮获取值
    
    
    

    启动项目后,使用chrome浏览器测试,却报出如下错误:

    nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘xxx‘错误的详细解决方法

    即nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'hiveTableName': was expecting ('true', 'false' or 'null')\n at [Source: java.io.PushbackInputStream@2cf94db9; line: 1, column: 15]的错误。

    2. 分析错误

    nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘xxx‘错误的详细解决方法,nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘xxx‘错误的详细解决方法,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,ChatGPT,方法,第3张
    (图片来源网络,侵删)

    正赶上最近ChatGPT比较火,可以借助它来帮我分析错误,如下图所示:

    nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘xxx‘错误的详细解决方法

    ChatGPT说我的错误是由于JSON数据格式不正确,或者解析代码有问题导致的。

    于是,使用postman来测试后端代码,是否存在问题,如下图所示:

    nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘xxx‘错误的详细解决方法

    后端代码能够成功接收postman的参数,也能够成功响应。

    nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘xxx‘错误的详细解决方法,nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘xxx‘错误的详细解决方法,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,ChatGPT,方法,第6张
    (图片来源网络,侵删)

    那么,由此可以断定,问题出现在前端代码中。

    根据网上的资料可知:发送ajax请求时,将js对象进行json字符串化处理,调用JSON.stringify()函数将js对象转换成字符串的格式发送到后台接口中。

    于是检查我的上述Ajax代码,果然没有使用JSON.stringify()函数修饰importTablesReq对象。

    3. 解决错误

    因为我的Ajax代码,没有使用JSON.stringify()函数修饰importTablesReq对象。

    因而,使用JSON.stringify()函数修饰importTablesReq对象即可,如下代码所示:

    function importTables() {
        const importTablesReq = {
            "hiveTableName": "project",
            "tableImportType": "1",
            "pkColumn": "id",
            "incrementColumn": "projectname",
            "cron": "0 0 11 * * ?",
            "datasetId": 2
        };
        $.ajax({
            url: 'http://localhost:8080/.../importTables.do',
            type: 'post',
            headers: {
                "x-userid": 1
            },
            data: JSON.stringify(importTablesReq),
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                console.log("data=", data)
            }
        });
    }
    

    nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘xxx‘错误的详细解决方法

    如是修改,便能成功调用导入hive表的接口,如下图所示:

    nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘xxx‘错误的详细解决方法

    4. 文末总结

    通过对nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'hiveTableName': was expecting ('true', 'false' or 'null')错误的分析与解决,可以清楚地知道如何解决该类错误。

    报出该类错误,即nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'xxx': was expecting ('true', 'false' or 'null'), 这种情况一般是由于发送ajax请求时,data选项传入的数据参数类型错误。

    当传入js对象类型时会出现如上错误,因为springMVC数据转换器无法解析并转换js对象的属性值到 java对象中,所以就会报以上异常错误。

    因而,需要调用JSON.stringify()函数将js对象转换成字符串的格式发送到后台接口中,才能避免或解决这种错误。


免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,1237人围观)

还没有评论,来说两句吧...

目录[+]