0

I am using Spring Framework 4.1.0 and Spring HATEOAS 0.16.0 to develop both a Spring web application and a Spring test client for that application.

The test client has a statement like:

ResponseEntity<Resource<Calculation>> response = restTemplate.exchange(
  calculationsUri,
  HttpMethod.POST,
  new HttpEntity<Calculation>(calculation),
  new ParameterizedTypeReference<Resource<Calculation>>()
);

...wherein Calculation is a POJO with Jackson annotations (for example, @JsonProperty).

Without CURIEs, that RestTemplate.exchange() invocation succeeds: response.getBody().getLinks() returns a non-null non-empty instance of List<Link>.

My web application has non-standard link relations, for example, "sub-calculations". I want to use CURIEs.

With CURIEs, that RestTemplate.exchange() invocation fails: The response-deserialization code throws org.springframework.http.converter.HttpMessageNotReadableException, caused by com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: 'Could not read JSON: Unrecognized field "name" (class org.springframework.hateoas.Link), not marked as ignorable (one known property "href"])'

In particular, Jackson fails to deserialize the CURIE(s) from the _links map in the response JSON to the List<Link>-typed field org.springframework.hateoas.ResourceSupport.links. The response JSON looks like:

{
  "_links" : {
    "self" : {
      "href" : "..."
    },
    "myNamespace:sub-calculations" : [ {
      "href" : "..."
    }, {
      "href" : "..."
    } ],
    "curies" : [ {
      "href" : ".../{rel}",
      "name" : "myNamespace",
      "templated" : true
    } ]
  }
}

How may I use RestTemplate.exchange() to obtain a resource whose HAL+JSON ("application/hal+json") representation uses CURIEs?

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
  • Does the client include Spring HATEOAS? – a better oliver Oct 31 '14 at 08:38
  • @zeroflagL: Yes, the client includes Spring HATEOAS. – Christian Conti-Vock Oct 31 '14 at 16:07
  • I don't think Spring hateoas is compatible with Spring 4 yet. – Jonathan W Oct 31 '14 at 18:20
  • Along the lines of your question... why do you need to use CURIEs? They don't add functionality to HAL (other than to just shorten URIs to improve readability), plus support has been very spotty for CURIEs around all of the HATEOAS frameworks. Traverson and Spring HATEOAS are no exception. – Jonathan W Oct 31 '14 at 18:24
  • @JonathanW: I don't _need_ to use CURIEs, but I want to use them for the benefits described [under "API Discoverability"](http://stateless.co/hal_specification.html), and so that I can change my link-relation URIs, since the client will retrieve links from the `_links` map via "`myNamespace:sub-calculations`" instead of via "`http://example.com/docs/rels/sub-calculations`". – Christian Conti-Vock Oct 31 '14 at 20:13
  • I didn't find any deserialization support for curies. Which makes sense considering that Spring HATEOAS is a server side library. What's more: `Curie` isn't a public class, so you can't use it, even if it had been deserialized. – a better oliver Nov 01 '14 at 14:36
  • 1
    CURIEs do not actually give you that advantage. The CURIE name is local to the JSON document and can be anything. Think of it working exactly as an XML namespace prefix. The only thing a CURIE does is improve readability for the API developer. – Jonathan W Nov 02 '14 at 04:39

1 Answers1

0

looks like the library simply doesn't support the name field of HAL link object https://datatracker.ietf.org/doc/html/draft-kelly-json-hal-06#section-5.5 ...doesn't really have anything to do with CURIE's. You should open an issue with that library to support all the fields of HAL link objects.

As far as CURIE @CCCV in your example they key is AWALYS myNamespace:sub-calculations no matter if CURIE is present or not. CURIE just lets you dereference to a URL that SHOULD link to documentation. It's kinda weird, and i see implementations get it wrong all the time thinking the URI is what matters. see https://groups.google.com/d/msg/hal-discuss/lt0CnC3eev4/YinN1Us54KcJ i'm not saying i agree with it..but that's how it's supposed to be

Community
  • 1
  • 1
Chris DaMour
  • 3,650
  • 28
  • 37