srnjak-testing-json

Utility for decorating JAX-RS responses with HATEOAS data.

License

License

Categories

Categories

hate Data Data Formats Hypermedia Types
GroupId

GroupId

com.srnjak
ArtifactId

ArtifactId

srnjak-hateoas
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

srnjak-testing-json
Utility for decorating JAX-RS responses with HATEOAS data.
Project URL

Project URL

https://github.com/srnjak/srnjak-hateoas
Source Code Management

Source Code Management

https://github.com/srnjak/srnjak-hateoas

Download srnjak-hateoas

How to add to project

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

Dependencies

compile (2)

Group / Artifact Type Version
javax.xml.bind : jaxb-api jar 2.3.1
org.projectlombok : lombok jar 1.18.10

provided (3)

Group / Artifact Type Version
javax.json : javax.json-api jar 1.1.4
javax.json.bind : javax.json.bind-api jar 1.0
javax.ws.rs : javax.ws.rs-api jar 2.1.1

test (20)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter jar 5.5.2
org.mockito : mockito-core jar 3.1.0
org.mockito : mockito-junit-jupiter jar 3.0.0
org.glassfish : javax.json jar 1.1.4
org.eclipse : yasson jar 1.0.5
org.apache.commons : commons-lang3 jar 3.9
jakarta.xml.bind : jakarta.xml.bind-api jar 2.3.2
junit : junit jar 4.12
org.junit.vintage : junit-vintage-engine jar 5.5.2
com.srnjak : srnjak-testing-json jar 1.0.1
org.xmlunit : xmlunit-core jar 2.6.3
org.xmlunit : xmlunit-assertj jar 2.6.3
org.glassfish.jersey.core : jersey-server jar 2.29.1
org.glassfish.jersey.containers : jersey-container-servlet jar 2.29.1
org.glassfish.jersey.inject : jersey-hk2 jar 2.29.1
org.glassfish.jersey.media : jersey-media-json-jackson jar 2.29.1
com.sun.istack : istack-commons-runtime jar 3.0.8
com.sun.xml.bind : jaxb-impl jar 2.3.2
org.glassfish.jersey.media : jersey-media-jaxb jar 2.29.1
org.glassfish.jersey.test-framework.providers : jersey-test-framework-provider-jdk-http jar 2.29.1

Project Modules

There are no modules declared in this project.

Srnjak HATEOAS library

Hateoas utility for JAX-RS environment. It's goal is to decorate an existent entity with data used for hypermedia.

At the moment it supports application/hal+json and application/hal+xml media types only.

Build

The library uses Maven as build tool.

To build and install into local repository use the following command:

mvn clean install

Usage

For creating hateoas output, all you need is to add the library as your project dependency.

Mark resources to produce one or more of the supported media types. Wrap an object or collection of object into the specific hypermedia model and set it as entity on response.

If there is unsupported media type listed, unwrapped object will be written in a endpoint response.

Single object

@Path("/single")
@GET
@Produces({                                                            // #1
        MediaType.APPLICATION_JSON,
        MediaType.APPLICATION_XML,
        HalMediaType.APPLICATION_HAL_JSON,
        HalMediaType.APPLICATION_HAL_XML})
public Response get() {

    MyEntity myEntity = ... ;

    EntityModel<MyEntity> entityModel = new EntityModel<>(myEntity);   // #2
    entityModel.addLink(Link.builder()                                 // #3
            .relation(IanaLinkRelation.SELF)
            .href("http://www.example.com/")
            .build());
    
    return Response.ok(entityModel).build();                           // #4
}
  1. List the supported media types which endpoint is able to produce.
  2. Wrap object into EntityModel (which is subclass of HypermediaModel).
  3. Add some links to the model.
  4. Set the wrapping model as entity to the response.

Collection

@GET
@Produces({                                                            // #1
          MediaType.APPLICATION_JSON,
          MediaType.APPLICATION_XML,
          HalMediaType.APPLICATION_HAL_JSON,
          HalMediaType.APPLICATION_HAL_XML})
public Response getAll() {

    List<MyEntity> myEntities = ... ;

    List<EntityModel<TestEntity>> emList = myEntities.stream()         // #2
            .map(e -> {
                EntityModel<TestEntity> entityModel = new EntityModel<>(e);
                entityModel.addLink(Link.builder()
                        .relation(IanaLinkRelation.SELF)
                        .href("http://www.example.com/" + e.getId())
                        .build());
                return entityModel;
            })
            .collect(Collectors.toUnmodifiableList());

    CollectionModel<MyEntity> collectionModel =                        // #3
            new GenericJaxrsCollectionModel<>(
                    emList, new GenericType<>(){});

    collectionModel.addLink(                                           // #4
            Link.builder()
                    .relation(IanaLinkRelation.SELF)
                    .href("http://www.example.com/")
                    .build());

    return Response.ok(collectionModel).build();                       // #5
}
  1. List the supported media types which endpoint is able to produce.
  2. Convert list of objects into list of hypermedia models.
  3. Wrap list of hypermedia models into CollectionModel (which is subclass of HypermediaModel).
  4. Add some links to the model.
  5. Set the wrapping model as entity to the response.

Versions

Version
1.0.0