无法读取 JSON:无法识别的字段“_links”-Android REST HATEOAS 客户端
Could not read JSON: Unrecognized field "_links" - Android REST HATEOAS client
我正在关注 this tutorial 的 Android REST 客户端,但我没有使用问候语服务,而是使用了我自己的 REST HATEOAS 服务,其中 returns HAL+JSON格式:
"name" : "new task",
"description" : "this is new task",
"_links" : {
"self" : {
"href" : "http://test/tasks/1"
},
"item" : {
"href" : "http://test/tasks/1/item"
},
}
我在运行时遇到这个错误:
Could not read JSON: Unrecognized field "_links"
(class com.test.mobile.model.Task), not marked as ignorable
(3 known properties: "name", "item", "description"])
在 MainActivity 的这一行:
Task task = restTemplate.getForObject(url, Task.class);
我会 copy/paste 所有代码,但它与教程中的代码相同,除了它有这两个 类 而不是问候语:
public class Task {
private String name;
private String description;
private Item item;
public Task() {
}
/*getters and setters*/
}
public class Item {
private String name;
private Set<Task> tasks = new HashSet<Task>(0);
public Item() {
}
/*getters and setters*/
}
我正在寻找 HATEOAS 的一些教程或代码,但没有找到任何相关内容。
如何修改 spring 的代码以便能够解析 HATEOAS _links?
您没有“_links”字段,您需要忽略它或添加一点 class 来获取该对象。
// Add this and you will get error on "self" that is not mark as ignore:
public class _links {
}
如何解决?只需添加另一个嵌套 class。
请注意它很快变得丑陋,我刚刚解释了这个想法。
真正的解决方案是 JsonIgnoreProperties 注释,它暂时写在这里 。
我正在关注 this tutorial 的 Android REST 客户端,但我没有使用问候语服务,而是使用了我自己的 REST HATEOAS 服务,其中 returns HAL+JSON格式:
"name" : "new task",
"description" : "this is new task",
"_links" : {
"self" : {
"href" : "http://test/tasks/1"
},
"item" : {
"href" : "http://test/tasks/1/item"
},
}
我在运行时遇到这个错误:
Could not read JSON: Unrecognized field "_links"
(class com.test.mobile.model.Task), not marked as ignorable
(3 known properties: "name", "item", "description"])
在 MainActivity 的这一行:
Task task = restTemplate.getForObject(url, Task.class);
我会 copy/paste 所有代码,但它与教程中的代码相同,除了它有这两个 类 而不是问候语:
public class Task {
private String name;
private String description;
private Item item;
public Task() {
}
/*getters and setters*/
}
public class Item {
private String name;
private Set<Task> tasks = new HashSet<Task>(0);
public Item() {
}
/*getters and setters*/
}
我正在寻找 HATEOAS 的一些教程或代码,但没有找到任何相关内容。
如何修改 spring 的代码以便能够解析 HATEOAS _links?
您没有“_links”字段,您需要忽略它或添加一点 class 来获取该对象。
// Add this and you will get error on "self" that is not mark as ignore:
public class _links {
}
如何解决?只需添加另一个嵌套 class。 请注意它很快变得丑陋,我刚刚解释了这个想法。
真正的解决方案是 JsonIgnoreProperties 注释,它暂时写在这里 。