S
S
ScRsa2016-06-30 07:22:33
Java
ScRsa, 2016-06-30 07:22:33

How to deserialize an object received from a Spring Data Rest service?

Spring Data Rest returns HATEOAS JSON, respectively, references to internal objects are passed by links:

public class Filial {
    private long id;
    private String name;
}

public class User {
    private long id;
    private String name;
    @JoinColumn(...)
    private Filial filial;
}

{
  "_embedded": {
    "users": [
      {
        "id": 1,
        "name": "test1",        
        "_links": {
          "self": {
            "href": "http://localhost/users/1"
          }
          "filial": {
            "href": "http://localhost/users/1/filial"
          }
        }
      }
    ]
  }
  "_links": { ... },
  "page": { ... }
}

the question is how to fully collect a user on the frontend?
using FeignClient
@FeignClient(url = "http://localhost/", name = "user-client")
public interface UserClient {
    @RequestMapping(method = GET, value = "/users")
    Resources<User> getAll();
}

at the output I get a list of users, but filial=null...
filialId is also not passed anywhere, a link to the branch is obtained only in the link

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
ScRsa, 2016-06-30
@ScRsa

The docs say to use @Projection. But for some reason it only works if excerptProjection is explicitly specified in the
docs.spring.io/spring-data/rest/docs/current/refer...

A
Alexander Kosarev, 2016-06-30
@jaxtr

So, that's the point of REST/HATEOAS - you don't get the associated object, just a link to it. Those. after receiving the user, you need to get a branch using the link from _links.filial.
If this approach does not suit you, then you can implement your own behavior of the REST service.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question