HATEOAS with HAL for Java. Create hypermedia APIs by easily serializing your Java models into HAL JSON.

Related tags

Testing hate
Overview

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.doorgroupId>
    <artifactId>hateartifactId>
    <version>v1r4t5version>
  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"
          }
        }
      }
    ]
  }
}

Comments
  • NullPointer from some HalLink methods if created from URI

    NullPointer from some HalLink methods if created from URI

    When a HalLink is created from a URI it uses a Boolean member variable templated to track this by setting it to null

    When later calling isTemplated or something that calls it (getHrefAsUri or getHrefAsTemplate) a null pointer is thrown as isTemplated's signature tries to return a primitive boolean

    bug help wanted 
    opened by ghost 5
  • Bad type on operand stack

    Bad type on operand stack

    Model

    package com.kwetter.models
    
    import black.door.hate.HalRepresentation
    import black.door.hate.HalResource
    
    import java.io.Serializable
    import java.net.URI
    import java.util.*
    import javax.json.bind.annotation.JsonbProperty
    import javax.json.bind.annotation.JsonbTransient
    import javax.persistence.*
    
    @Entity
    @Table(name = "tweets")
    data class Tweet(
            @Id
            @GeneratedValue(strategy = GenerationType.IDENTITY)
            var id: Long? = null,
            var text: String,
            var date: Date) : HalResource, Serializable {
        override fun representationBuilder(): HalRepresentation.HalRepresentationBuilder {
            return HalRepresentation.builder()
                    .addProperty("id", id)
                    .addProperty("text", text)
                    .addProperty("data", date)
                    .addProperty("username", username())
                    .addProperty("profile_image", profileImage())
                    .addLink("self", this)
        }
    
        override fun location(): URI {
            return URI("/api/tweets/{$id}")
        }
    
        @JsonbTransient
        @ManyToOne(fetch = FetchType.EAGER)
        lateinit var user: User
    
        @JsonbProperty("username")
        fun username(): String {
            return user.username
        }
    
        @JsonbProperty("profile_image")
        fun profileImage(): String {
            return user.image
        }
    
        constructor(text: String, date: Date) : this(null, text, date)
    }
    

    Error

    java.lang.VerifyError: Bad type on operand stack
    Exception Details:
      Location:
        com/fasterxml/jackson/databind/SerializerProvider.<init>(Lcom/fasterxml/jackson/databind/SerializerProvider;)V @15: putfield
      Reason:
        Type 'com/fasterxml/jackson/databind/ser/std/NullSerializer' (current frame, stack[1]) is not assignable to 'com/fasterxml/jackson/databind/JsonSerializer'
      Current Frame:
        bci: @15
        flags: { }
        locals: { 'com/fasterxml/jackson/databind/SerializerProvider', 'com/fasterxml/jackson/databind/SerializerProvider' }
        stack: { 'com/fasterxml/jackson/databind/SerializerProvider', 'com/fasterxml/jackson/databind/ser/std/NullSerializer' }
      Bytecode:
        0x0000000: 2ab7 0001 2ab2 0002 b500 032a b200 04b5
        0x0000010: 0005 2ab2 0006 b500 072a 01b5 0008 2a01
        0x0000020: b500 0e2a 01b5 0009 2a01 b500 0d2a bb00
        0x0000030: 0a59 b700 0bb5 000c 2a2b b400 03b5 0003
        0x0000040: 2a2b b400 11b5 0011 2a2b b400 05b5 0005
        0x0000050: 2a2b b400 07b5 0007 2a2b b400 10b5 0010
        0x0000060: b1    
    

    Endpoint

    ...
        @GET
        @Path("/{id}")
        fun single(@PathParam("id") id: Long): HalRepresentation {
    
            val tweet = tweetService.find(id)
    
            val hal = tweet.asEmbedded()
    
            return hal
        }
    ...
    

    I'm using kotlin btw

    opened by yooouuri 3
  • Bump junit from 4.12 to 4.13.1

    Bump junit from 4.12 to 4.13.1

    Bumps junit from 4.12 to 4.13.1.

    Release notes

    Sourced from junit's releases.

    JUnit 4.13.1

    Please refer to the release notes for details.

    JUnit 4.13

    Please refer to the release notes for details.

    JUnit 4.13 RC 2

    Please refer to the release notes for details.

    JUnit 4.13 RC 1

    Please refer to the release notes for details.

    JUnit 4.13 Beta 3

    Please refer to the release notes for details.

    JUnit 4.13 Beta 2

    Please refer to the release notes for details.

    JUnit 4.13 Beta 1

    Please refer to the release notes for details.

    Commits
    • 1b683f4 [maven-release-plugin] prepare release r4.13.1
    • ce6ce3a Draft 4.13.1 release notes
    • c29dd82 Change version to 4.13.1-SNAPSHOT
    • 1d17486 Add a link to assertThrows in exception testing
    • 543905d Use separate line for annotation in Javadoc
    • 510e906 Add sub headlines to class Javadoc
    • 610155b Merge pull request from GHSA-269g-pwp5-87pp
    • b6cfd1e Explicitly wrap float parameter for consistency (#1671)
    • a5d205c Fix GitHub link in FAQ (#1672)
    • 3a5c6b4 Deprecated since jdk9 replacing constructor instance of Double and Float (#1660)
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 2
  • Bump jackson.version from 2.9.8 to 2.10.0

    Bump jackson.version from 2.9.8 to 2.10.0

    ⚠️ Dependabot is rebasing this PR ⚠️

    If you make any changes to it yourself then they will take precedence over the rebase.


    Bumps jackson.version from 2.9.8 to 2.10.0.

    Updates jackson-databind from 2.9.8 to 2.10.0

    Commits

    Updates jackson-datatype-jsr310 from 2.9.8 to 2.10.0

    Updates jackson-datatype-jdk8 from 2.9.8 to 2.10.0

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot ignore this [patch|minor|major] version will close this PR and stop Dependabot creating any more for this minor/major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 2
  • Bump jackson.version from 2.9.8 to 2.10.0.pr1

    Bump jackson.version from 2.9.8 to 2.10.0.pr1

    Bumps jackson.version from 2.9.8 to 2.10.0.pr1.

    Updates jackson-databind from 2.9.8 to 2.10.0.pr1

    Commits

    Updates jackson-datatype-jsr310 from 2.9.8 to 2.10.0.pr1

    Updates jackson-datatype-jdk8 from 2.9.8 to 2.10.0.pr1

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot ignore this [patch|minor|major] version will close this PR and stop Dependabot creating any more for this minor/major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 2
  • Bump jackson.version from 2.9.8 to 2.10.0.pr3

    Bump jackson.version from 2.9.8 to 2.10.0.pr3

    Bumps jackson.version from 2.9.8 to 2.10.0.pr3.

    Updates jackson-databind from 2.9.8 to 2.10.0.pr3

    Commits

    Updates jackson-datatype-jsr310 from 2.9.8 to 2.10.0.pr3

    Updates jackson-datatype-jdk8 from 2.9.8 to 2.10.0.pr3

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot ignore this [patch|minor|major] version will close this PR and stop Dependabot creating any more for this minor/major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 1
  • Upgrade maven plugins

    Upgrade maven plugins

    Upgrade the various maven plugins in use

    maven-compiler-plugin 3.3 -> 3.8.0 jacoco-maven-plugin 0.7.5.201505241946 -> 0.8.3 nexus-staging-maven-plugin 1.6.6 -> 1.6.8 maven-source-plugin 2.4 -> 3.0.1 maven-javadoc-plugin 2.10.3 -> 3.0.1

    opened by rhowe-gds 1
  • Missing tag for v1r4t0 release

    Missing tag for v1r4t0 release

    Hi, would it be possible for you to tag the v1r4t0 release? It allow things like automatic changelog generation by tools such as Dependabot.

    I think it might be b22d761241676c116b5289556008dad4c7dcf479 but I'm not sure.

    opened by rhowe-gds 1
  • Addressed #16

    Addressed #16

    #16

    Changed optional fields in HalLink to use java Optional. Added HalLinkSerializer to fix odd issue where Optional is not serialized correctly despite jackson module. Made indentation uniform

    opened by nrktkt 1
  • Add default method for HalResource

    Add default method for HalResource

    a default representationBuilder method in a sub-interface of HalResource would build representations based on the existing jackson config for that class.

    help wanted 
    opened by nrktkt 1
  • Bump jackson-databind from 2.12.6.1 to 2.13.4.1

    Bump jackson-databind from 2.12.6.1 to 2.13.4.1

    Bumps jackson-databind from 2.12.6.1 to 2.13.4.1.

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 0
Releases(v1r4t4)
Owner
null
🤖 Unleash the full power of test.ai into your Java Selenium tests

The test.ai selenium SDK is a simple library that makes it easy to write robust cross-browser web tests backed by computer vision and artificial intelligence.

test.ai 5 Jul 15, 2022
🤖 Unleash the full power of test.ai into your Java Appium tests

The test.ai Appium SDK is a simple library that makes it easy to write robust cross-platform mobile application tests backed by computer vision and ar

test.ai 9 Jun 4, 2022
A powerful open source test automation platform for Web Apps, Mobile Apps, and APIs

A powerful open source test automation platform for Web Apps, Mobile Apps, and APIs. Build stable and reliable end-to-end tests @ DevOps speed.

Testsigma Technologies Inc 466 Dec 31, 2022
The engine for the classification of texts into negative, neutral or positive sentiment (sentiment analysis)

Umigon-core The classification engine for sentiment analysis. The basic operations are: decompose the text into n-grams create a version of the n-gram

Clement Levallois 3 Jun 23, 2022
Aesthetirat, your neighborhood pet rat that is inside your PC!

aesthetirat Aesthetirat, your neighborhood pet rat that is inside your PC! Disclaimer: This is for educational purposes, and I do not take responsibil

Gavin 34 Dec 2, 2022
A sample repo to help you handle basic auth for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to handle basic auth for automation test in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Windows - htt

null 12 Jul 13, 2022
A sample repo to help you clear browser cache with Selenium 4 Java on LambdaTest cloud. Run your Java Selenium tests on LambdaTest platform.

How to clear browser cache with Selenium 4 Java on LambdaTest cloud Prerequisites Install and set environment variable for java. Windows - https://www

null 12 Jul 13, 2022
A sample repo to help you run automation test in incognito mode in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to run automation test in incognito mode in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Windows - htt

null 12 Jul 13, 2022
A sample repo to help you handle cookies for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to handle cookies for automation test in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Windows - https:

null 13 Jul 13, 2022
A sample repo to help you set geolocation for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to set geolocation for automation test in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Windows - https

null 12 Jul 13, 2022
A sample repo to help you capture JavaScript exception for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to capture JavaScript exception for automation test in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Wi

null 12 Jul 13, 2022
A sample repo to help you find an element by text for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to find an element by text for automation test in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Windows

null 12 Jul 13, 2022
A sample repo to help you emulate network conditions in Java-selenium automation test on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to emulate network conditions in Java-selenium automation test on LambdaTest Prerequisites Install and set environment variable for java. Windows

null 12 Jul 13, 2022
CodeSheriff is a simple library that helps you in writing JUnit tests that check the quality of your code

CodeSheriff is a simple library that helps you in writing JUnit tests that check the quality of your code. For example, CodeSheriff may fail because you have methods in your code that have more than X lines of code, or that have complexity greater than Y.

Maurício Aniche 62 Feb 10, 2022
High-level contextual steps in your tests for any reporting tool

Xteps High-level contextual steps in your tests for any reporting tool. License Maven Central Javadoc Xteps Xteps Allure Xteps ReportPortal How to use

Evgenii Plugatar 8 Dec 11, 2022
A Java architecture test library, to specify and assert architecture rules in plain Java

ArchUnit is a free, simple and extensible library for checking the architecture of your Java code. That is, ArchUnit can check dependencies between pa

TNG Technology Consulting GmbH 2.5k Jan 2, 2023
Never debug a test again: Detailed failure reports and hassle free assertions for Java tests - Power Asserts for Java

Scott Test Reporter for Maven and Gradle Get extremely detailed failure messages for your tests without assertion libraries, additional configuration

Dávid Csákvári 133 Nov 17, 2022
TCP Chat Application - Java networking, java swing

TCP-Chat-Application-in-Java TCP Chat Application - Java networking, java swing Java – Multithread Chat System Java Project on core Java, Java swing &

Muhammad Asad 5 Feb 4, 2022