首页 > Web开发 > 详细

JSON parse error: Unrecognized token 'xxx': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false');

时间:2021-01-20 13:09:10      阅读:51      评论:0      收藏:0      [点我收藏+]

问题现象

  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

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

 

 

  

  

 

 

  

JSON parse error: Unrecognized token 'xxx': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false');

原文:https://www.cnblogs.com/ShaoShuai20160203/p/14301987.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!