HalBuilder / halbuilder-core

HalBuilder Core
38 stars 26 forks source link

Halbuilder is a simple Java API for generating and consuming HAL documents conforming to the HAL Specification.

travis codecov

Generating Local Resources

Map<String,Object> friend = HashMap.of("name", "Mike", "age", 36);
ResourceRepresentation<Map<String,Object>> owner =
  ResourceRepresentation.create("http://example.com/mike", friend)
    .withLink("td:friend", "http://example.com/mamund")

Map<String,Object> todoMeta = HashMap.of(
  "created_at", "2010-01-16", "updated_at", "2017-06-13",
  "summary", "An example list");

ResourceRepresentation<Map<String,Object>> halResource =
  ResourceRepresentation.create("http://example.com/todo-list", todoMeta)
    .withLink("td:search", "/todo-list/search;{searchterm}")
    .withLink("td:description", "/todo-list/description")
    .withRepresentation("td:owner", owner);

JsonRepresentationWriter jsonRepresentationWriter =
  JsonRepresentationWriter.create();

ByteString representation = jsonRepresentationWriter.print(accountRepWithLinks);
System.out.println(representation.utf8());

Reading Local Resources

JsonRepresentationReader jsonRepresentationReader =
  JsonRepresentationReader.create();

ResourceRepresentation<ByteString> representation =
  jsonRepresentationReader.read(
    new InputStreamReader(Some.class.getResourceAsStream("/test.json")));

// or as a type

ResourceRepresentation<Person> personRepresentation =
  jsonRepresentationReader.read(
    new InputStreamReader(Some.class.getResourceAsStream("/test.json")),
    Person.class);

Apache Maven

HalBuilder is deployed to Apache Maven Central under the following coordinates:

<dependency>
  <groupId>com.theoryinpractise</groupId>
  <artifactId>halbuilder5</artifactId>
  <version>5.0.1</version>
</dependency>

Website

More documentation is available from the main website at gotohal.net.

Development Forum

Email support and discussion is available on the development forum.