Could not read JSON: Unrecognized field "_links" - Android REST HATEOAS client

1.4k views Asked by At

I was following this tutorial for Android REST client, but instead of using greeting service, I used my own REST HATEOAS service which returns HAL+JSON in this format:

"name" : "new task",
"description" : "this is new task",
"_links" : {
"self" : {
  "href" : "http://test/tasks/1"
},
"item" : {
  "href" : "http://test/tasks/1/item"
},
}

I get this error at runtime:

Could not read JSON: Unrecognized field "_links" 
(class com.test.mobile.model.Task), not marked as ignorable 
(3 known properties: "name", "item", "description"])

At this line in MainActivity:

Task task = restTemplate.getForObject(url, Task.class);

I would copy/paste all the code, but it's identical to one from tutorial, except it has this two classes instead of greeting:

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*/
}

I was looking for some tutorial or code with HATEOAS but found nothing of relevance.

How can I modify my code for spring to be able to parse HATEOAS _links?

2

There are 2 answers

3
Remy On BEST ANSWER

You dont have a field for "_links" you need to ingnore it OR add a little class that would take that object.

// Add this and you will get error on "self" that is not mark as ignore:
public class _links {

}

How you can solve it? Simply Add another nested class. be aware it became ugly fast, i have just explained the idea.

0
withoutOne On

the real solution is JsonIgnoreProperties annotation which written on here https://stackoverflow.com/a/26614217/4173048 for now.