srnjak-testing-json

Utility for decorating JAX-RS responses with HATEOAS data.

Лицензия

Лицензия

Категории

Категории

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

Группа

com.srnjak
Идентификатор

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

srnjak-hateoas
Последняя версия

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

1.0.0
Дата

Дата

Тип

Тип

jar
Описание

Описание

srnjak-testing-json
Utility for decorating JAX-RS responses with HATEOAS data.
Ссылка на сайт

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

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

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

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

Скачать srnjak-hateoas

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

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

Зависимости

compile (2)

Идентификатор библиотеки Тип Версия
javax.xml.bind : jaxb-api jar 2.3.1
org.projectlombok : lombok jar 1.18.10

provided (3)

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

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

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

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

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.

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

Версия
1.0.0