java - 从 localhost 导入列表时,人们如何修复反序列化错误?
问题描述
我正在尝试将我的 json 列表放入我的代码中,但我不断收到此错误。
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:840)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1206)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1592)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1570)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:286)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4202)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3205)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3188)
at dev.lightt.Application.main(Application.java:34)
我的代码是
public static final String POSTS_API_URL = "http://localhost:3000/Leaders";
public static void main(String[] args) throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.header("accept", "application/json")
.uri(URI.create(POSTS_API_URL))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// parse JSON
ObjectMapper mapper = new ObjectMapper();
List<Post> posts = mapper.readValue(response.body(), new TypeReference<>() {
});
posts.forEach(System.out::println)
我正在检查 restful API 教程,但它没有提到这一点。
我的一点 Json 看起来像
{
"UserId":1,
"Id":1,
"Title":"Life",
"Body":"Man suffers only because he takes seriously what the gods made for fun."
},
{
"UserId":1,
"Id":2,
"Title":"Nature",
"Body":"Without birth and death, and without the perpetual transmutation of all the forms of life, the world would be static, rhythm-less, undancing, mummified."
},
这是邮政类代码。这是说没有使用getter和setter。唯一没有说的是 getUserId()、getTitle() 和 getId()。
public class Post {
private int id;
private String title;
private String body;
private int userId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
@Override
public String toString() {
return "Post{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
'}';
}
}
}
解决方案
映射区分大小写,因此您可以:
- 将您的 json 更改为
{
"userId":1,
"id":1,
"title":"Life",
"body":"Man suffers only because he takes seriously what the gods made for fun."
}
- 映射 POJO 中的属性
@JsonProperty("Id")
private int id;
@JsonProperty("Title")
private String title;
@JsonProperty("Body")
private String body;
@JsonProperty("UserId")
private int userId;
推荐阅读
- python-3.x - 如何修复对烧瓶中的样式表不起作用的继承问题
- php - 如何使用 PHP Mailer 向 gmail 发送电子邮件?
- database - 在 Neo4j 中寻找最短路径函数应用于现实生活中的问题
- javascript - React Native - 检测带有圆角的Android设备?
- python - 在链表中创建递归函数 findValue()
- excel - 如何计算2列的单元格变化
- xml - 在 Robot Framework 中寻找具有相同元素名称的不同文本
- python-3.x - 对字符串列表进行数字排序
- python-3.x - 如何在 Python3 中掷骰子
- python - 如何使用 Python 和 Matplotlib 绘制数值函数?