blackdoor hate

HATEOAS with HAL for Java

License

License

Categories

Categories

hate Data Data Formats Hypermedia Types
GroupId

GroupId

black.door
ArtifactId

ArtifactId

hate
Last Version

Last Version

v1r4t5
Release Date

Release Date

Type

Type

jar
Description

Description

blackdoor hate
HATEOAS with HAL for Java
Project URL

Project URL

https://github.com/blackdoor/hate
Source Code Management

Source Code Management

https://github.com/blackdoor/hate

Download hate

How to add to project

<!-- https://jarcasting.com/artifacts/black.door/hate/ -->
<dependency>
    <groupId>black.door</groupId>
    <artifactId>hate</artifactId>
    <version>v1r4t5</version>
</dependency>
// https://jarcasting.com/artifacts/black.door/hate/
implementation 'black.door:hate:v1r4t5'
// https://jarcasting.com/artifacts/black.door/hate/
implementation ("black.door:hate:v1r4t5")
'black.door:hate:jar:v1r4t5'
<dependency org="black.door" name="hate" rev="v1r4t5">
  <artifact name="hate" type="jar" />
</dependency>
@Grapes(
@Grab(group='black.door', module='hate', version='v1r4t5')
)
libraryDependencies += "black.door" % "hate" % "v1r4t5"
[black.door/hate "v1r4t5"]

Dependencies

compile (5)

Group / Artifact Type Version
black.door : blackdoor-lib jar v4r3t1
com.fasterxml.jackson.core : jackson-databind jar 2.10.0
com.fasterxml.jackson.datatype : jackson-datatype-jsr310 jar 2.10.0
com.fasterxml.jackson.datatype : jackson-datatype-jdk8 jar 2.10.0
com.damnhandy : handy-uri-templates jar 2.1.7

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.18.6

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.json : json jar 20180813

Project Modules

There are no modules declared in this project.

hate

HATEOAS with HAL for Java. Create hypermedia APIs by easily serializing your Java models into HAL JSON.
More info in the wiki.

Gitter
Build Status
Codacy Badge
Codecov
Javadocs
Maven Central


Install with Maven

<dependencies>
  <dependency>
    <groupId>black.door</groupId>
    <artifactId>hate</artifactId>
    <version>v1r4t5</version>
  </dependency>
</dependencies>

Basic usage

Implement the HalResource interface in your model by implementing the location() and representationBuilder() methods. For example:

public class Order implements HalResource{

	private int id;
	private double total;
	private String currency;
	private String status;
	//note: Basket and Customer implement HalResource
	private Basket basket;
	private Customer customer;

	...

	@Override
	public URI location(){
		return new URI("/orders/" + id);
	}
	
	@Override
	public HalRepresentationBuilder representationBuilder() {
		return HalRepresentation.builder()
				.addProperty("total", total)
				.addProperty("currency", currency)
				.addProperty("status", status)
				.addLink("basket", basket)
				.addLink("customer", customer)
				.addLink("self", this);
	}
}	

Now to get the HalRepresentation of an Order object, simply do HalRepresentation hal = myOrder.asEmbedded(). You can serialize hal using hal.serialize() or with Jackson (eg. new ObjectMapper().writeValueAsString(hal))

The result would look like this:

{
  "total": 30,
  "currency": "USD",
  "status": "shipped",
  "_links": {
    "basket": {
      "href": "/baskets/97212"
    },
    "self": {
      "href": "/orders/123"
    },
    "customer": {
      "href": "/customers/7809"
    }
  }
}

Paginated Collections

To get a paginated HAL representation of a REST collection, simply use HalRepresentation.paginated(String, String, Stream, long, long).
For example:

Collection<Order> orders;

HalRepresentation hal = HalRepresentation.paginated(
	"orders", // the name of the resource collection
	"/orders", // the path the resource collection can be found at
	orders.stream(), // the resources
	pageNumber,
	pageSize // the number of resources per page
	).build();

Would give you something like this:

{
  "_links": {
    "next": {
      "href": "/orders?page=2"
    },
    "self": {
      "href": "/orders"
    }
  },
  "_embedded": {
    "orders": [
      {
        "total": 30,
        "currency": "USD",
        "status": "shipped",
        "_links": {
          "basket": {
            "href": "/baskets/97212"
          },
          "self": {
            "href": "/orders/123"
          },
          "customer": {
            "href": "/customers/7809"
          }
        }
      },
      {
        "total": 20,
        "currency": "USD",
        "status": "processing",
        "_links": {
          "basket": {
            "href": "/baskets/97213"
          },
          "self": {
            "href": "/orders/124"
          },
          "customer": {
            "href": "/customers/12369"
          }
        }
      }
    ]
  }
}

black.door

Versions

Version
v1r4t5
v1r4t4
v1r4t3
v1r4t2
v1r4t1
v1r4t0
v1r3t2
v1r3t1
v1r3t0
v1r2t3
v1r2t2
v1r2t1
v1r2t0