Unrecognized token ‘xx‘: was expecting (JSON String, Number, Array, Object or token ‘null‘, ‘true‘..

迷南。 2024-04-06 12:48 187阅读 0赞

一,业务场景

与第三方对接,需要调第三方接口,用postmen都可以正常请求,但是用java代码写的post请求就报错。

二、报错信息

2、JSON parse error: Unrecognized token ‘license’: was expecting (JSON String, Number, Array, Object or token ‘null’, ‘true’ or ‘false’); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token ‘license’: was expecting (JSON String, Number, Array, Object or token ‘null’, ‘true’ or ‘false’)\n at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 9]

三问题原因:

很明显就是入参“license”解析报错,具体而言,和接口的 @RequestBody关键字有关,在后台参数反序列化过程中出错了。

四、post请求代码

这里是参考我之前写的一个post请求

我这里的写法会有两个问题,对应上面的两个报错

java实现http,post请求_凌抆莂的博客-CSDN博客

  1. public static String send(String url, Map<String, String> map, String encoding) throws Exception {
  2. String body = "";
  3. // 创建httpclient对象
  4. CloseableHttpClient client = HttpClients.createDefault();
  5. // 创建post方式请求对象
  6. HttpPost httpPost = new HttpPost(url);
  7. // 装填参数
  8. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  9. if (map != null) {
  10. for (Entry<String, String> entry : map.entrySet()) {
  11. nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
  12. }
  13. }
  14. // 设置参数到请求对象中
  15. httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding));
  16. // 设置header信息
  17. // 指定报文头【Content-type】、【User-Agent】
  18. httpPost.setHeader("Content-type", "application/json;charset=UTF-8");
  19. // 执行请求操作,并拿到结果(同步阻塞)
  20. CloseableHttpResponse response = client.execute(httpPost);
  21. // 获取结果实体
  22. HttpEntity entity = response.getEntity();
  23. if (entity != null) {
  24. // 按指定编码转换结果实体为String类型
  25. body = EntityUtils.toString(entity, encoding);
  26. }
  27. EntityUtils.consume(entity);
  28. // 释放链接
  29. response.close();
  30. return body;
  31. }

五、解决办法

将httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding));

改为httpPost.setEntity(new StringEntity(str, encoding));

修改后的请求

  1. public static void main(String[] args) {
  2. String urlPath = "xxxxxx";
  3. JSONObject param = new JSONObject();
  4. JSONObject jsonObject = new JSONObject();
  5. List list = new ArrayList();
  6. param.put("license", "111");
  7. list.add(jsonObject);
  8. param.put("record", list);
  9. TdLog.error("1111========"+param);;
  10. String post = send(urlPath, param.toJSONString(), "UTF-8");
  11. }
  12. public static String send(String url, String str, String encoding) {
  13. String body = "";
  14. try {
  15. // 创建httpclient对象
  16. CloseableHttpClient client = HttpClients.createDefault();
  17. // 创建post方式请求对象
  18. HttpPost httpPost = new HttpPost(url);
  19. // 设置参数到请求对象中
  20. httpPost.setEntity(new StringEntity(str, encoding));
  21. // 设置header信息
  22. // 指定报文头【Content-type】、【User-Agent】
  23. httpPost.setHeader("Content-type", "application/json;charset=UTF-8");
  24. // 执行请求操作,并拿到结果(同步阻塞)
  25. CloseableHttpResponse response = client.execute(httpPost);
  26. // 获取结果实体
  27. HttpEntity entity = response.getEntity();
  28. if (entity != null) {
  29. // 按指定编码转换结果实体为String类型
  30. body = EntityUtils.toString(entity, encoding);
  31. }
  32. EntityUtils.consume(entity);
  33. // 释放链接
  34. response.close();
  35. return body;
  36. } catch (Exception e1) {
  37. e1.printStackTrace();
  38. return "";
  39. }
  40. }

参考:JSON parse error: Unrecognized token ‘xxx’: was expecting (JSON String, Number, Array, Object or token ‘null’, ‘true’ or ‘false’); - 少帅府 - 博客园

发表评论

表情:
评论列表 (有 0 条评论,187人围观)

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

相关阅读