'How to parse JSON+HAL response into POJO

Recently I am investigating Spring Boot. I did a simple JPA + DATA + REST spring-boot application. For this initial creation...

    User user = new User();
    user.setFirstName("fname");
    user.setLastName("lname");
    user.setId(1);
    List<Item> items = new ArrayList<Item>();
    Item item = new Item();
    item.setName("item1");
    item.setPattern("pattern1");
    Item item2 = new Item();
    item2.setName("item2");
    item2.setPattern("pattern2");
    itemRepository.save(item);
    itemRepository.save(item2);
    items.add(item);
    items.add(item2);
    user.setItems(items);
    userRepository.save(user);

...the REST interface provides HAL specific responses like this.

$ curl http://localhost:8080/user
{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/user{?page,size,sort}",
      "templated" : true
    },
    "search" : {
      "href" : "http://localhost:8080/user/search"
    }
  },
  "_embedded" : {
    "user" : [ {
      "firstName" : "fname",
      "lastName" : "lname",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/user/1"
        },
        "items" : {
          "href" : "http://localhost:8080/user/1/items"
        }
      }
    } ]
  },
  "page" : {
    "size" : 20,
    "totalElements" : 1,
    "totalPages" : 1,
    "number" : 0
  }
}

Now I want to parse JSON+HAL response to get the User object, but I cannot find a clear example of doing this scenario in java.

Could anyone help me? Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source