zoukankan      html  css  js  c++  java
  • JSON parse error: Unrecognized token 'xxx': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false');

    问题现象

      Spring Boot项目中,访问后台接口时,报错:

        [ ERROR] [2021-01-20 11:15:15.214] com.xxx.test.handle.ExceptionHandle [44] [http-nio-127.0.0.1-8855-exec-6] [handleException] - JSON parse error: Unrecognized token 'xxx': was expecting       (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.core.JsonParseException:

          Unrecognized token 'xxx': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
          at [Source: (PushbackInputStream); line: 1, column: 19]

    原因分析

      很明显,这是访问接口的参数格式不对造成的,具体而言,和接口的 @RequestBody关键字有关,在后台参数反序列化过程中出错了。

      接口如下:

        

    1     @PostMapping("")
    2     public ResponseEntity<ResultEntity<Long>> test(@RequestBody TestData TestData){
    3         Long startTime = System.currentTimeMillis();
    4     }

      

    解决方法

      方法1:前台是通过ajax提交时,

        使用JSON.stringify(data) 序列化请求参数

      方法2:如果是通过HttpClient模拟请求

        在给HTTPPOST实例添加请求参数时,就不能使用 UrlEncodedFormEntity的方式添加,这次遇到的坑就是使用了这个

     1         // 封装post请求参数
     2         if (null != paramMap && paramMap.size() > 0) {
     3             List<NameValuePair> nvps = new ArrayList<NameValuePair>();            
     4             // 通过map集成entrySet方法获取entity
     5             Set<Entry<String, Object>> entrySet = paramMap.entrySet();
     6             // 循环遍历,获取迭代器
     7             for (Entry<String, Object> mapEntry : entrySet) {
     8                  nvps.add(new BasicNameValuePair(mapEntry.getKey(), JSON.toJSONString(mapEntry.getValue())));
     9             }
    10 11 // 为httpPost设置封装好的请求参数 12 try { 13 httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); 14 } catch (UnsupportedEncodingException e) { 15 e.printStackTrace(); 16 }

        修改为 UrlEncodedFormEntity的父类 StringEntity 即可,修改后的代码如下:

        

     1  // 封装post请求参数
     2         if (null != paramMap && paramMap.size() > 0) {
     3             JSONObject jsonObject = new JSONObject();           
     4             // 通过map集成entrySet方法获取entity
     5             Set<Entry<String, Object>> entrySet = paramMap.entrySet();
     6             // 循环遍历,获取迭代器
     7             for (Entry<String, Object> mapEntry : entrySet) {
     8                 jsonObject.put(mapEntry.getKey(), mapEntry.getValue());
     9             }
    10             
    11             // 为httpPost设置封装好的请求参数
    12             try {
    13                 httpPost.setEntity(new StringEntity(jsonObject.toString()));
    14             } catch (UnsupportedEncodingException e) {
    15                 e.printStackTrace();
    16             }
    View Code

        修改后,重新启动即可正常访问。  

      

      

     

      

  • 相关阅读:
    ViewSwitcher使用范例
    TextSwitcher,译为文字转换器控件
    android开发中系统自带语音模块的使用
    android string.xml %问题
    Android动画解析--XML
    Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形)
    Android操作系统11种传感器介绍
    开发移动app与服务器端session的状态管理与交互
    android开发分辨率问题解决方案
    android获取手机录
  • 原文地址:https://www.cnblogs.com/ShaoShuai20160203/p/14301987.html
Copyright © 2011-2022 走看看