关于fasterxml-jackson发生Can not deserialize instance of异常原因验证

这两天线上有大量的java.lang.IllegalArgumentException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

at [Source: N/A; line: -1, column: -1]错误发生。

有经验的人一看,就知道是对象属性转换发生异常了。为了把这个错误的根本原因找到。只能上代码模拟了。

/**
* Created by changle on 17/1/9.
*/
@Slf4j
public class JSONTest {
public static void main(String[] args) {
testAtoB();
//testAtoB() 发生:Can not deserialize instance of com.test.JSONTest$Hobby out of START_ARRAY token

testBtoA();
//testBtoA() 发生:Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
}

public static void testAtoB(){
List<Hobby> hobbies = new ArrayList<>();
Random random = new Random();
for(int i=0;i<3;i++){
Hobby hobby = new Hobby(random.nextInt(),"测试名称","测试类型",random.nextInt(100));
hobbies.add(hobby);
}
StudentA studentA = new StudentA();
studentA.setAge(23);
studentA.setFromCity(true);
studentA.setMoney(3000);
studentA.setName("张三");
studentA.setHobbies(hobbies);
try {
String str = JSON.json(studentA);
log.info("str={}",str);
//list转换单个projo
StudentB studentB = JsonUtil.jsonObject(str, StudentB.class);
log.info("studentB.name:{}",studentB.getName());
} catch (Exception e) {
e.printStackTrace();
}
}

public static void testBtoA(){
Random random = new Random();
Hobby hobby = new Hobby(random.nextInt(), "测试名称", "测试类型", random.nextInt(100));
StudentB studentB2 = new StudentB();
studentB2.setAge(23);
studentB2.setFromCity(true);
studentB2.setMoney(3000);
studentB2.setName("张三");
studentB2.setHobbies(hobby);
String str2 = null;
try {
str2 = JSON.json(studentB2);
//单个projo转换list
StudentA studentA2 = JsonUtil.jsonObject(str2, StudentA.class);
log.info("studentB.name:{}", studentA2 == null ? "" : studentA2.getName());
} catch (IOException e) {
e.printStackTrace();
}
}

@Data
public static class StudentA{
private String name;
private int age;
private long money;
private boolean isFromCity;
private List<Hobby> hobbies;
}

@Data
public static class StudentB{
private String name;
private int age;
private long money;
private boolean isFromCity;
private Hobby hobbies;
}

@Data
public static class Hobby{
private long hId;
private String hName;
private String type;
private int score;

public Hobby(){}

public Hobby(long hId,String hName,String type,int score){
this.hId = hId;
this.hName = hName;
this.type = type;
this.score = score;
}
}
}

结论:

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
该错误是因为目标类属性keyX需要ArrayList类型的,待转换的json串里属性名keyX对应的,不是一个ArrayList集合,而是单个POJO。

Can not deserialize instance of com.test.JSONTest$Hobby out of START_ARRAY token
该错误是因为目标类属性keyX需要JSONTest$Hobby类型的,待转换的json串里属性名keyX对应的,不是一个POJO对象,而是ArrayList集合。

---------------------
作者:常乐_smile
来源:CSDN
原文:https://blog.csdn.net/li396864285/article/details/54287859/
版权声明:本文为博主原创文章,转载请附上博文链接!

昨天与同事联调http接口,准备用RestTemplate 进行请求,调用post请求,,但是参数死活传不进去:

测试对比:

先贴出自己的接口方法如下

public class User {

private String loginname;
// @Size(min=6 ,max= 20 ,message = "{items.password.size}")
private String password;
// @Size(min=3,max = 10,message = "{items.name.size}")
private String username;

// @NotNull
private Integer age;

public User() {

}

public String getLoginname() {
return loginname;
}

public void setLoginname(String loginname) {
this.loginname = loginname;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

@Override
public String toString() {
return "User{" +
"loginname='" + loginname + '\'' +
", password='" + password + '\'' +
", username='" + username + '\'' +
'}';
}
}
controller 中的get post测试接口

@RequestMapping(value = "/userone",method = RequestMethod.GET)
@Validation
public @ResponseBody String logUserOne(
@Validated User user) {

return user.toString();

}

@PostMapping(value = "/userone1")
@ResponseBody
public String logUserOne1(@RequestBody User user) {

return user.toString();

}

下面为postman请求:结果正常

准备在代码中进行测试:遇到如下错误

写如下测试方法:

1.直接传map,运行结果可以出来
result = restTemplate.postForObject(url, map, String.class);
System.out.println("map:"+result);

2.此种传法报错
org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type

result = restTemplate.postForObject(url, paramMapt, String.class);
System.out.println("paramMapt:"+result);

3.此种传法也报错
org.springframework.web.client.HttpClientErrorException: 400 Bad Request

result = restTemplate.postForObject(url, formEntity, String.class);

经过查看 spring相关的jar包版本为4.3.7
以前看到其他同事写的都是使用的第三种方法,发现运行的相当正常,不知现在自己使用怎么这么费劲,希望有知道的朋友给与答疑解惑

@Test
public void getbdc23(){

RestTemplate restTemplate = new RestTemplate();
String result =null;
Map<String,String> map = new HashMap<>();
map.put("loginname", "spw2444");
map.put("password", "passpasss");
map.put("username", "username");

HttpHeaders headers = new HttpHeaders();
headers.setContentType( MediaType.APPLICATION_JSON );

// headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.add("Accept","application/json");
MultiValueMap<String, String> paramMapt = new LinkedMultiValueMap<String, String>() {
{
this.add("loginname", "loginname");
this.add("password", "passpasss");
this.add("username", "username");
}
};

HttpEntity<MultiValueMap<String, String>> formEntity = new HttpEntity<MultiValueMap<String, String>>(paramMapt, headers);
String url = "http://localhost:8083/validated/userone1";

// ResponseEntity<String> result = restTemplate.postForEntity(url, formEntity, String.class,map);

// System.out.println(result.getBody());
User user = new User();
user.setLoginname("longinna");
user.setPassword("pass");
user.setUsername("name");
result = restTemplate.postForObject(url, map, String.class);
System.out.println("map:"+result);
result = restTemplate.postForObject(url, paramMapt, String.class);
System.out.println("paramMapt:"+result);
result = restTemplate.postForObject(url, formEntity, String.class);
System.out.println("formEntity:"+result);
}

---------------------
作者:phoebe_IT
来源:CSDN
原文:https://blog.csdn.net/u012448083/article/details/82490683
版权声明:本文为博主原创文章,转载请附上博文链接!

项目中使用Ribbon传递form表单数据遇到一个数据转换问题:
使用fastJson转换LinkedMultiValueMap时报错

String jsonString = "{\"ownerId\":\"186\",\"ownerRole\":\"AGENT\",\"settleCardId\":null,\"group\":\"PAY\"," +
"\"organization\":null}";
LinkedMultiValueMap params = JSON.parseObject(jsonString, LinkedMultiValueMap.class);
System.out.println(params);

错误信息:

Exception in thread "main" com.alibaba.fastjson.JSONException: java.lang.String cannot be cast to java.util.List
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:630)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:348)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:252)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:471)
at com.yp.ribbonconsume.TestMain.main(TestMain.java:47)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.List
at org.springframework.util.LinkedMultiValueMap.put(LinkedMultiValueMap.java:38)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:447)
at com.alibaba.fastjson.parser.deserializer.MapDeserializer.deserialze(MapDeserializer.java:64)
at com.alibaba.fastjson.parser.deserializer.MapDeserializer.deserialze(MapDeserializer.java:41)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:626)
... 4 more

原因是因为LinkedMultiValueMap的value是List<V>类型,赶时间写了一个比较笨的方法

String jsonString = "{\"ownerId\":\"186\",\"ownerRole\":\"AGENT\",\"settleCardId\":null,\"group\":\"PAY\"," +
"\"organization\":null}";
JSONObject object = JSONObject.parseObject(jsonString);
Iterator it = object.keySet().iterator();
StringBuilder sb = new StringBuilder("{");
while (it.hasNext()) {
String key = (String) it.next();
Object value = object.getString(key);
if (value == null) {
sb.append(JSON.toJSONString(key)).append(":").append(value).append(",");
} else {
List<String> list = Collections.singletonList(value.toString());
sb.append(JSON.toJSONString(key)).append(":").append(JSON.toJSONString(list)).append(",");
}
}
sb.append("}");
sb.deleteCharAt(sb.length()-2);
System.out.println(sb.toString());
LinkedMultiValueMap params = JSON.parseObject(sb.toString(), LinkedMultiValueMap.class);
System.out.println(params);

如果各位有好的方法,烦请告之,不慎感激!!!

作者:就怕是个demo
链接:https://www.jianshu.com/p/cd9fde614174
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

LinkedMultiValueMap

一个key对应多个value

添加一个Key对应一个Value的:void add(K, V);
添加一个Key对应多个Value的:void add(K, List);
设置一个Key对应一个Value的:void set(K, V);
设置一个Key对应多个Value的:void set(K, List);
移除一个Key和它对应的Value:List remove(K);
清除所有Value的:void clear();
拿到所有Key的集合:Set keySet();
拿到所有的值:List values();
拿到一个Key对应的某个值:V getValue(K, index);
拿到一个Key对应的所有Value:List getValues(K);
拿到MultiValueMap的大小:int size();
判断是否为空MultiValueMap:boolean isEmpty();
判断是否包含某个Key:boolean containKey(K);
LinkedMultiValueMap<String, String> linkedMultiValueMap = new LinkedMultiValueMap<>();

linkedMultiValueMap.add("name", "t1");
linkedMultiValueMap.add("name", "t2");
linkedMultiValueMap.add("name", "t3");

for (Entry<String, List<String>> entry : linkedMultiValueMap.entrySet()) {

System.out.println(entry.getKey() + entry.getValue());
// List<String> value = entry.getValue();

}
打印结果为 name[t1, t2, t3]
---------------------
作者:麻辣小鸡丝儿
来源:CSDN
原文:https://blog.csdn.net/weixin_41074208/article/details/80598751
版权声明:本文为博主原创文章,转载请附上博文链接!

关于fasterxml-jackson发生Can not deserialize instance of异常原因验证的更多相关文章

  1. jackon - com.fasterxml.jackson.databind.exc.InvalidDefinitionException && UnrecognizedPropertyException: Unrecognized field 异常

    在用jackson解析json数据是碰到的问题 1.首先是InvalidDefinitionException 测试发现可能是目标类中无无参数构造方法导致异常. 添加无参构造方法后发现前一个异常解决但 ...

  2. 解决用户自生成meta导入kylin后报错问题Can not deserialize instance of java.lang.String[] out of VALUE_STRING token

    报错栈: -- ::, ERROR [http-bio--exec-] cube.CubeManager: : Error during load cube instance, skipping : ...

  3. com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field 异常

    分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_30739519) 1.1.1. 前言 近期在使用ObjectMapper对象将jso ...

  4. com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "ExceptionId"

    com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Exception ...

  5. com.fasterxml.jackson.core.JsonParseException: Unexpected character

    com.fasterxml.jackson.core.JsonParseException: Unexpected )): was expecting double-quote to start fi ...

  6. com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "FileSize"

    请求阿里云的OSS接口图片信息,返回json格式的数据,通过ObjectMapper将json转为Image对象时候报错转换失败 将json转对象的代码: String jsonStr = " ...

  7. com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'user'

    nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'user' 可能错误原因: ...

  8. JSON parse error: Cannot deserialize instance of `int` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc

    代码程序: @PostMapping("selectById") @ResponseBody public Result selectById(@RequestBody int i ...

  9. JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of j

    异常信息如下: JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token ...

随机推荐

  1. JAVA中内部类(匿名内部类)访问的局部变量为什么要用final修饰?

    本文主要记录:在JAVA中,(局部)内部类访问某个局部变量,为什么这个局部变量一定需要用final 关键字修饰? 首先,什么是局部变量?这里的局部是:在方法里面定义的变量. 因此,内部类能够访问某局部 ...

  2. 安装bazel(syntaxnet依赖工具)

    1.简介   Bazel是一个类似于Make的工具,是Google为其内部软件开发的特点量身定制的工具,如今Google使用它来构建内部大多数的软件.它的功能有诸多亮点: 多语言支持:目前Bazel默 ...

  3. [C#.net]将null值插入SQL Server的Datetime

    之前处理SQL Server可以为空时间字段总是设置时间的最小值和最大值,今天才发现也可以把null用C#的代码插入到sql内 使用可空的值类型,代码如下 public DateTime? Servi ...

  4. LOJ-10105(欧拉回路模板,套圈法,递归)

    题目链接:传送门 思路: (1)用邻接表存储有向图和无向图,有向图和无向图的每条边均站两个单元,无向图有正向边和反向边的区分. (2)有向图有欧拉回路:所有点的入度=出度: 无向图有欧拉回路:所有点的 ...

  5. winform改变启动界面

    我们知道,有时做个小项目什么的,一般从登录开始,再到主页,再到其他业务,如果做到其他页面功能,调试时还要从登录页面一个个点进去,明显的降低开发进度. 这时,我们可以直接将目标界面改为启动页面即可. u ...

  6. Javascript上传超大文件实例

    因业务需求需要向伺服器上传大于1GB以上的视频文件,其实网上也能找到很多大文件上传的第三方组件,问题是要么用起来相当不方便,总出现一些bug,要么收费太贵(费用几千,甚至还限定使用数量),最终自己开发 ...

  7. 进程控制(Note for apue and csapp)

    1. Introduction We now turn to the process control provided by the UNIX System. This includes the cr ...

  8. 精选!15个必备的VSCode插件

    Visual Studio Code 是由微软开发的一款免费.跨平台的文本编辑器.由于其卓越的性能和丰富的功能,它很快就受到了大家的喜爱. 就像大多数 IDE 一样,VSCode 也有一个扩展和主题市 ...

  9. 【接口时序】5、QSPI Flash的原理与QSPI时序的Verilog实现

    一. 软件平台与硬件平台 软件平台: 1.操作系统:Windows-8.1 2.开发套件:ISE14.7 3.仿真工具:ModelSim-10.4-SE 4.Matlab版本:Matlab2014b/ ...

  10. spring框架学习笔记2:配置详解

    简单写一个类做示例: package bean; public class User { private String name; private Integer age; public String ...