面临JSON解析问题,无法反序列化类型' java.lang '的值.来自数组值的字符串jackson.databind



在我的邮差请求中包含数组时面临JSON解析问题:

{
"id" : "0",
"number":["2222", "3333"]
}

pojo:

public class User {
@Getter @Setter private String      id;
@Getter @Setter  private String[]   number;
//i've also tried these:
@Getter @Setter  private List<String>   number;
@Getter @Setter  private ArrayList<String>  number;
}

控制器:

@PostMapping("/user")
public ResponseEntity<Object> getUser(@RequestBody(required=true)User user) {
跟踪:

"trace": "org.springframework.http.converter.HttpMessageNotReadableException: 
JSON parse error: Cannot deserialize value of type `java.lang.String` from Array value (token `JsonToken.START_ARRAY`); 
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: 
Cannot deserialize value of type `java.lang.String` from Array value (token `JsonToken.START_ARRAY`)
at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 3, column: 13] 

我也有同样的问题。事实证明,当我从测试JS脚本发送消息时,Stomp.js没有自动序列化作为消息体传递给JSON的对象,它所做的只是将其转换为字符串并发送"[Object object]"作为有效载荷,Jackson将其解释为一个数组,因为[在前面,并且立即失败,因为它没有期望接收数组。

client.publish({
destination: '/app/publish',
body: {
id: 0,
content: 'I am very frustrated',
sender: 'test_user',
chat: 0
}
}); // => "[Object object]"

在将对象作为有效负载传递之前显式地手动序列化该对象所做的事情:

client.publish({
destination: '/app/publish',
body: JSON.stringify({
id: 0,
content: 'I am very frustrated',
sender: 'test_user',
chat: 0
})
}); // => {id: 0, ...}

问题一直是Javascript。

为你的类添加一个无参数构造函数,并将其命名为Serializable

最新更新