Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Client for POSTing complex entities to a Spring Data REST / HATEOAS service

From what I can tell, there are provided means for converting a complex object to proper HAL format. This is of course leveraged in marshalling the objects in the framework itself. Resource and Link objects, etc.

For the sake of a use-case: Company 1 is an existing Company in my system. I want to add a new Employee that works for Company 1

Below is an example Employee object that you'd receive from a Spring Data REST based service. Spring HATEOAS also provides the means to construct these objects yourself.

{
    "id": null,
    "firstName": "bZWthNFk",
    "lastName": "GtTnrqka",
    "loginId": "zTk5rT",
    "active": true,
    "_links": {
        "company": {
            "href": "http://localhost/companies/1";
        }
    }
}

However, this seems to not work for POSTing the object. As I understand it, that same object would have to be POSTed as:

{
    "id": null,
    "firstName": "bZWthNFk",
    "lastName": "GtTnrqka",
    "loginId": "zTk5rT",
    "active": true,
    "company": "http://localhost/companies/1"
}

As far as I can tell, there are no means provided by either the HATEOAS or Data REST project to produce this object for posting to a valid HAL based service, either via RestTemplate or some other means. In fact, I can't find any means of readily POSTing a complex object without some hand-marshalling. Am I wrong in assuming this?

How is one supposed to build out a valid Java SDK for service-to-service communication that leverages HATEOAS principles without this tooling to actually POST objects reliably?


Long story short, I want to post this object without having to hand serialize the URIs for associations.

public class Employee {
    private Integer id;
    @NotNull
    private Company company;
    private String firstName;
    private String lastName;
}

I've created the following improvement request in reference to this:

https://jira.spring.io/browse/SPR-12678

like image 885
bvulaj Avatar asked Jan 27 '15 22:01

bvulaj


People also ask

What is used for exposing spring data repositories over REST using Spring data REST?

Spring Data REST can be used to expose HATEOAS RESTful resources around Spring Data repositories. Without writing a lot of code, we can expose RESTful API around Spring Data Repositories.

Should I use spring HATEOAS?

Building REST APIs that follow HATEOAS principle is not easy. So Spring HATEOAS provides some APIs that ease the creation of hypermedia links in API responses (links in JSON documents). It works well with Spring MVC and Spring Data JPA and supports for hypermedia formats like HAL (Hypertext Application Language).

What is the use of spring HATEOAS?

Spring HATEOAS provides some APIs to ease creating REST representations that follow the HATEOAS principle when working with Spring and especially Spring MVC. The core problem it tries to address is link creation and representation assembly.

How do I configure clients with spring HATEOAS’s new release?

With Spring HATEOAS’s recent 1.1.0.M3 release, we bring you a new way to configure clients! The first step in building hypermedia-based services may be configuring your server, which Spring HATEOAS has provided for a long time through its @EnableHypermediaSupport (). The next major step is building a client that can parse that hypermedia output.

Does spring HATEOAS require servlet context?

In the Spring HATEOAS project, we do not require Servlet Context and concatenate the path variable to the base URI. Instead of this, Spring HATEOAS offers three abstractions for creating the URI: ContrrollerLinkBuilder, Link, and Resource Support. We can use these abstractions to create metadata, which associates with the resource representation.

What is the dependency of HATEOAS in spring?

Most important dependency is spring-hateoas. To implement HATEOAS, we would need to include related resources in the response. Instead of Student we use a return type of EntityModel<Student>. EntityModel is a simple class wrapping a domain object and allows adding links to it.

What is the default response format for spring HATEOAS representationmodel?

By default, Spring hateoas generated responses are in application/hal+json format. It is the default mediatype even if we pass application/json as well. In HAL, the _links entry is a JSON object. The property names are link relations and each value is single or multiple links. 2. Spring HATEOAS RepresentationModel Example


1 Answers

The approach you suggested should actually work, provided you use at least version 2.0 of Spring Data REST.

You should also have an association resource like http://app.com/employee/10/company. You can PUT a new link to that location using the media type text/uri-list or remove the company from Employee with a DELETE.

UDATE

It seems I didn't address your main concern, that was clarified by your update and comments. So let's take your Employee class that has an association with a Customer.

As you can see from the JSON response you posted, the data structure that the REST API works with doesn't contain a Customer object (or Company in that case), only a link. A client would usually work with the data structure defined by the API. So customerwould be link in the first place and there would be no need for serializing an object to a link.

If the client uses a different data structure internally, then some kind of conversion is necessary anyway. But the reason would be the different structure, not HAL or association links.

like image 116
a better oliver Avatar answered Oct 03 '22 20:10

a better oliver