GithubHelp home page GithubHelp logo

otto-de / edison-hal Goto Github PK

View Code? Open in Web Editor NEW
40.0 16.0 10.0 938 KB

A library to parse and render application/hal+json documents in Java

License: Apache License 2.0

Java 93.11% HTML 2.75% JavaScript 4.14%

edison-hal's Introduction

Edison HAL

Library to produce and consume application/hal+json representations of REST resources using Jackson.

Build Status Maven Central Javadoc codecov

1. Documentation

2. About

At otto.de, microservices should only communicate via REST APIs with other microservices. HAL is a nice format to implement the HATEOAS part of REST. Edison-HAL is a library to make it easy to produce and consume HAL representations for your REST APIs.

Currently, there are only a couple of libraries supporting HAL and even less that support the full media type including all link properties, curies (compact URIs) and embedded resources.

Spring HATEOAS, for example, is lacking many link properties, such as title, name, type and others.

3. Features

Creating HAL representations:

  • Links with all specified attributes like rel, href, profile, type, name, title, etc. pp.
  • Embedded resources, even deeply nested
  • Curies in links and embedded resources
  • Generation of HAL representations using Jackson annotations

Parsing HAL representations:

  • Mapping application/hal+json to Java classes using Jackson. This also works for deeply nested embedded items.
  • Simple domain model to access links, embedded resources etc.
  • Curies are automatically resolved: Given a curi with name=ex and href=http://example.com/rels/{rel}, links and embedded items can be access either in curied format ex:foo, or expanded format http://exampl.com/rels/foo

Traversion of HAL representations:

  • Simple client-side navigation through linked and embedded REST resources using Traverson API
  • Embedded resources are transparantly used, if present.
  • Curies are resolved transparantly, too. Clients of the Traverson API do not need to know anything about curies or embedded resources.

edison-hal's People

Contributors

agebhar1 avatar dependabot[bot] avatar frankbregulla1111 avatar gsteinacker avatar martinei avatar mediamarco avatar pfeuffer avatar shisheng-1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

edison-hal's Issues

Traverson is ignoring embedded items

Traverson should not follow links if the linked items are embedded. At least using relative links and maybe also with curies links, this is currently not the case.

Do not catch / hide Exceptions thrown while traversing resources

In 1.0.0, Exceptions are caught by the Traverson. Clients have to check for Exceptions using Traverson.getLastError(), which is not too obvious.
Beginning with 2.0.0, the Traverson is exposing the Exceptions (IOException) in the API, so clients have a better chance to add appropriate error handling.

Resolving relative URLs in Links

Provide support for resolving relative HREFs in links so the client does not have to worry about relative/absolute links in HAL representations.

Traverson: Specify Type of embedded items

Traverson.getResource: gets a resource as HalRepresentation
Traverson:getResourceAs(type): gets a resource as
Missing: specification of the type of embedded items.

Embedding items in embedded resources

While it is possible to embed items in embedded resources, CURIs in deeply embedded items are not handled properly. This is possibly leading to unresolvable curied links.

Add Traverson.follow() methods to bypass embedded resources.

In application/hal+json, linked resources may optionally be embedded into a resource. Right now, the Traverson is always using embedded resources, if present. While this is helpful in most situations, clients may want to bypass the embedded items and resolve links instead. For example, server might embed only a subset of a resource, while the client might need to get the full representation.

"_embedded" (single) resource object not supported?

Is it correct - when rendering HalRepresentation - that is not supported to have a single resource object in _embedded?

As per the spec:

The reserved "_embedded" property is OPTIONAL

It is an object whose property names are link relation types (as
defined by [RFC5988]) and values are either a Resource Object or an
array of Resource Objects.

edison-hal seems to only allow to set a list of resources in the _embedded property like:

protected HalRepresentation withEmbedded(final String rel, final List<? extends HalRepresentation> embeddedItems)

In some cases an explicit single resource is desired instead of an array.

Accessing custom JSON attributes

It is currently required to sub-class HalRepresentation, if custom JSON attributes are required.
Generating or at least reading additional attributes using a generic API would be nice. This way, it would be possible to implement Tools similar to the HAL Browser without having to know about such attributes.

Question: Use already existing domain-model?

I tried to understand edison. For me it seems that I need to write my own HalRepresentation. But I already have a domain-model. Is it possible to use this and add some specific mapping or do I need to reimplement (define Halpresentations with all properties) my domain model.

Globally outputting null values of hal represenations

HalRepresentation has a @JsonInclude(NON_NULL) annotation which causes to not include null properties of resources.

It would be nice to switch off this behaviour globally.

That said I can imagine that e.g. _embedded is not desired to be outputted when null.
Maybe it's better to move the @JsonInclude(NON_NULL) to particular fields of HalRepresentation?

Creation of Multiple Links Representation should be based on RelationType and not on number of elements of that type

Due to

if (links.size() > 1 || rel.equals("curies")) {

it is currently not possible to have a link relation type (other than curries) that should be represented as a multiple, if there are less than two elements of that relation type.

Example:
The custom link relation type "myType" should allow multiples.

HalRepresentation.withLinks(
   Link.link("myRelationType", "http://www.example.com/myResource/1"),
   Link.link("myRelationType", "http://www.example.com/myResource/1"))
/* Will produce:
...
"_links": {
  "myRelationType": [
    {
      "href":"http://www.example.com/myResource/1"
    },
    {
      "href":"http://www.example.com/myResource/2"
    }
  ]
}
...
*/

HalRepresentation.withLinks(
    Link.link("myRelationType", "http://www.example.com/myResource/1")) 
/* Will produce:
...
"_links": {
  "myRelationType": {
      "href":"http://www.example.com/myResource/1"
    }
}
...
Should produce: 

...
"_links": {
  "myRelationType": [
    {
      "href":"http://www.example.com/myResource/1"
    }
  ]
}
...
*/

remove duplicate curis from embedded resources

Hi,

I have an idea for improvment and I would like to know what you think.

I have multiple HalRepresentation which have rels which use curis. Representation A would like to embed representation B, both use rels which can use the same curi.

My HAL representation factory of B always includes a curi link. When I now serialize an Repr. A, the curi will be duplicated in the JSON.

At the moment embedded representations use the relRegistry of the parent, it would be nice to remove duplicated curis at the same time.

Paging of Items

The Traverson already makes it easy to iterate over items in a page. However, iterating over multiple /all pages and all items per page is not supported.
The Traverson should be refactored to support this.

Add support for traversing everything up to last hop

I've come across a new use case where I'd like Traverson to navigate the API but then delegate the last request to something else (in my case, Spring's MockMvc for assertions).
Currently, the only way to do that is by using something like

traverson.follow("item")
    .follow("parent")
    .follow("item")
// or one of the similar methods
    .getResource();
mockMvc.perform(put(traversion.currentContextUrl().toUri()))
    .andExpect(...);

Although this mostly does the right thing, it issues an unnecessary GET request for the last hop (which might fail for several reasons).

Would it be possible to add a traversalUrl() method (or something with a better name) that supports things like

traverson.follow("item")
    .follow("parent")
    .follow("item");
mockMvc.perform(put(traverson.traversalUrl().toUri()))
    .andExpect(...);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.