0

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?

Miljac
  • 2,045
  • 4
  • 19
  • 28

2 Answers2

2

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.

Remy
  • 1,053
  • 1
  • 19
  • 25
  • I know I can add _links class, but that doesn't seem like the right way to do it, more of a workaround – Miljac Jun 15 '15 at 05:33
  • 1
    @Miljac Nop, it's just a diifence style, in json.net & JS, you simply assign for "var". Anyway, I don't know about spring much, but Gson support getters and setters, maybe you could make a getter and parse it inside. (Anyway, I hope there is an bea way stright via spring, but i don't know this framwork well). – Remy Jun 15 '15 at 15:54
  • Since I implemented custom serializers which parse _links, I will accept this as right answer. – Miljac Jul 31 '15 at 16:04
0

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

withoutOne
  • 723
  • 1
  • 6
  • 14