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

关于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
版权声明:本文为博主原创文章,转载请附上博文链接!

 

    原文作者:沧海一滴
    原文地址: https://www.cnblogs.com/softidea/p/10670654.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞