blackdoor hate

HATEOAS with HAL for Java

Лицензия

Лицензия

Категории

Категории

hate Данные Data Formats Hypermedia Types
Группа

Группа

black.door
Идентификатор

Идентификатор

hate
Последняя версия

Последняя версия

v1r4t5
Дата

Дата

Тип

Тип

jar
Описание

Описание

blackdoor hate
HATEOAS with HAL for Java
Ссылка на сайт

Ссылка на сайт

https://github.com/blackdoor/hate
Система контроля версий

Система контроля версий

https://github.com/blackdoor/hate

Скачать hate

Как подключить последнюю версию

<!-- 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"]

Зависимости

compile (5)

Идентификатор библиотеки Тип Версия
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)

Идентификатор библиотеки Тип Версия
org.projectlombok : lombok jar 1.18.6

test (2)

Идентификатор библиотеки Тип Версия
junit : junit jar 4.12
org.json : json jar 20180813

Модули Проекта

Данный проект не имеет модулей.

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

Версии библиотеки

Версия
v1r4t5
v1r4t4
v1r4t3
v1r4t2
v1r4t1
v1r4t0
v1r3t2
v1r3t1
v1r3t0
v1r2t3
v1r2t2
v1r2t1
v1r2t0