See the Resource class from spring-hateaos.
It provides methods for extracting links from a response. However, since RestTemplate requires you to provide the class as a variable, I have not found any other way than subclassing the desired object and using it for RestTemplate.
Then you can look like this:
public class BerufResource extends Resource<Beruf> { } BerufResource resource = template.getForEntity("http://example.at/berufe/1", BerufResource.class); Beruf beruf = resource.getContent();
If you want to request a complete list, you need to pass the version of your array to RestTemplate:
BerufResource[] resources = template.getForEntity("http://example.at/berufe", BerufResource[].class); List<BerufResource> berufResources = Arrays.asList(resources); for(BerufResource resource : berufResources) { Beruf beruf = resource.getContent(); }
Unfortunately, we cannot write Resource<Beruf>.class , which defeats the whole purpose of the universal class, since we need to again create a subclass for each object. The reason for this is the erasure of styles. I read somewhere that they plan to introduce general support for RestTemplate, but I don't know any details.
Address retrieval of identifier from URL:
I would recommend using a different client-side model and replacing the type of the id field with a string and storing the entire URL in it. Thus, you can easily restore the entire object whenever you want, and you do not need to create URLs yourself. In any case, you will need the URL later if you plan to send POST requests to your API, as spring -hateaos requires you to send a link instead of an identifier. A typical POST request might look like this:
{ "firstname": "Thomas", "nachname": "Maier", "profession": "http://example.at/professions/1" }