Could not read document: Unrecognized token ‘xxx‘: was expecting (‘true‘, ‘false‘ or ‘null‘)错误的解决方法

这篇具有很好参考价值的文章主要介绍了Could not read document: Unrecognized token ‘xxx‘: was expecting (‘true‘, ‘false‘ or ‘null‘)错误的解决方法。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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表的接口,如下代码所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>content-type为application/json</title>
    <script src="js/jquery-3.4.1.js"></script>
    <script>
        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)
                }
            });
        }
    </script>
</head>
<body>
<button type="button" onclick="importTables()">点击按钮获取值</button>
</body>
</html>

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

Could not read document: Unrecognized token ‘xxx‘: was expecting (‘true‘, ‘false‘ or ‘null‘)错误的解决方法,免费专栏,javascript,后端,前端,ajax,postman

Could not read document: Unrecognized token 'hiveTableName': was expecting ('true', 'false' or 'null')\n at [Source: java.io.PushbackInputStream@2cf94db9; line: 1, column: 15];的错误。

2. 分析错误


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

Could not read document: Unrecognized token ‘xxx‘: was expecting (‘true‘, ‘false‘ or ‘null‘)错误的解决方法,免费专栏,javascript,后端,前端,ajax,postman

ChatGPT说我的错误是由于JSON 解析器无法识别hiveTableName这个标记,这标记是实体类ImportTablesBo的属性,也是Ajax中的传参字段。

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

Could not read document: Unrecognized token ‘xxx‘: was expecting (‘true‘, ‘false‘ or ‘null‘)错误的解决方法,免费专栏,javascript,后端,前端,ajax,postman

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

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

根据网上的资料可知:发送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)
        }
    });
}

Could not read document: Unrecognized token ‘xxx‘: was expecting (‘true‘, ‘false‘ or ‘null‘)错误的解决方法,免费专栏,javascript,后端,前端,ajax,postman

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

Could not read document: Unrecognized token ‘xxx‘: was expecting (‘true‘, ‘false‘ or ‘null‘)错误的解决方法,免费专栏,javascript,后端,前端,ajax,postman

4. 文末总结


通过对Could not read document: Unrecognized token 'hiveTableName': was expecting ('true', 'false' or 'null')错误的分析与解决,可以清楚地知道如何解决该类错误。

报出该类错误,即Could not read document: Unrecognized token 'xxx': was expecting ('true', 'false' or 'null'), 这种情况一般是由于发送ajax请求时,data选项传入的数据参数类型错误。

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

因而,需要调用JSON.stringify()函数将js对象转换成字符串的格式发送到后台接口中,才能避免或解决这种错误。文章来源地址https://www.toymoban.com/news/detail-756006.html

到了这里,关于Could not read document: Unrecognized token ‘xxx‘: was expecting (‘true‘, ‘false‘ or ‘null‘)错误的解决方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • [Bug0034] Git报错 cannot lock ref 'refs/heads/master': is at xxxx but expected xxx error: could not ...

    1、问题 error: update_ref failed for ref \\\'refs/heads/master\\\': cannot lock ref \\\'refs/heads/master\\\': is at 63654e79f7ae0f902731558b3ae6679a69db09e9 but expected ec20d6ffa52920358e54703f90100bedbca4c855 error: could not update refs/heads/master 2、场景 由于为了方便解决冲突获取到最新远程代码(并且本地有冲突代码不想要),直接删

    2024年02月16日
    浏览(37)
  • nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘xxx‘错误的详细解决方法

    今天写好 导入hive表 的接口,如下代码所示: 同时,使用 Ajax 调用 导入hive表 的接口,如下代码所示: 启动项目后,使用 chrome 浏览器测试,却报出如下错误: 即 nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token \\\'hiveTableName\\\': was expecting (\\\'true\\\', \\\'false\\\' or \\\'null\\\')

    2024年02月04日
    浏览(36)
  • xcode The document “...“ could not be saved

    Today when I tried to save a file on my project I get an error message saying: The document “nameOfFile.m” could not be saved. I tried reinstalling xcode but no luck. The file can be edited with other editors and I see the same behavior on all my projects as well as newly created projects. Any ideas? 解决:重新启动了 XCode(通过强制退出,而

    2024年02月08日
    浏览(37)
  • Unrecognized VM option ‘CMSParallelRemarkEnabled‘ Error: Could not create the Java Virtual Machine.

    演示分布式事务 Seata报:如下异常 Unrecognized VM option ‘CMSParallelRemarkEnabled’ Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. 错误原因: 1、表明在尝试启动 Seata Server 时遇到了 Java 虚拟机(JVM)配置的问题。具体来说,Unrecognized VM option ‘CM

    2024年04月17日
    浏览(40)
  • 【git】error: RPC failed; curl 28 OpenSSL SSL_read: Connection was reset, errno 10054 fatal: expected

    因为这几天需要用git ,我不太会,所以学习一下😭 当我 git clone 的时候 结果报错 error: RPC failed; curl 28 OpenSSL SSL_read: Connection was reset, errno 10054 fatal: expected flush after ref listing 后来查资料 说要在 git clone 前执行 原因是因为: 针对所有远程服务器全局执行,使git忽略ssl证书错误

    2024年02月11日
    浏览(50)
  • web项目启动报错:Document base XXX does not exist or is not a readable directory

    原因分析:旧web项目启动操作时留下的缓存导致的。 1. 删除tomcat根目录下的work或work目录下的Catalina文件夹 我的问题是这样解决的,有的问题还需进行第二步删除 2. 删除tomcat—》conf—》server.xml中无用的Context标签,改文件也可在eclipse中查找,如图。

    2024年02月15日
    浏览(48)
  • 解决export ‘default‘ (imported as ‘xxx‘) was not found in ‘xxx‘

    今天写代码时出现了问题,记录一下,源代码如下 编译时警告 试了很久最后发现是import语法问题  

    2024年02月04日
    浏览(34)
  • Plugin xxx was was not found in any of the following sources:

            最近打开AndroidStudio,经常出现如下异常:     尝试调整gradle版本,发现仍然不能解决,最后 通过降低app目录下build.gradle的  \\\"compileSdk\\\"和\\\"targetSdk\\\"版本,以及去掉buildToolsVersion解决。

    2024年02月12日
    浏览(45)
  • could not read ok from ADB Server

    ADB不能连接: 关闭防火墙可以解决。 保存退出,重启ADB连接即可。

    2024年02月11日
    浏览(26)
  • spring:Could not resolve placeholder ‘XXX‘ in value “${XXX}“

    1、背景 使用 @Value 注解从配置文件中读取值时出现\\\"Could not resolve placeholder\\\"很有可能是使用了多个PropertyPlaceholderConfigurer或者多个context:property-placeholder的原因。 2、原因 因为spring通过反射机制加载外部资源 context:property-placeholder location=\\\"classpath:XXX.properties\\\"/ 在探测到Spring容器中

    2024年02月11日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包