Lean and minimalistic Java EE library for creating Siren JSONP responses.
Library to use the Siren Hypermedia content type in enterprise Java. Can read and create entity objects from and to JSON programmatically. Also contains basic hypermedia client functionality to follow links and perform actions.
No external dependencies are needed. Contains only one (provided) dependency to the Java EE 7 API.
The current Siren version is 0.6.1
.
Usage:
<dependency> <groupId>com.sebastian-daschner</groupId> <artifactId>siren4javaee</artifactId> <version>1.2</version> </dependency>
To create JSON objects programmatically:
JsonObject bookEntity = Siren.createEntityBuilder() .addClass("book") .addProperty("isbn", book.getIsbn()) .addProperty("name", book.getName()) .addProperty("author", book.getAuthor()) .addProperty("availability", book.getAvailability().toString()) .addProperty("price", book.getPrice()) .addLink(bookUri, "self") .addAction(Siren.createActionBuilder() .setName("add-to-cart") .setTitle("Add book to cart") .setMethod(HttpMethod.POST) .setHref(shoppingCartUri) .setType(MediaType.APPLICATION_JSON) .addField("isbn", FieldType.TEXT) .addField("quantity", FieldType.NUMBER)).build();
To read JSON objects into Entity
objects:
EntityReader entityReader = Siren.createEntityReader(); Entity entity = entityReader.read(jsonObject);
To follow links and perform actions from a Siren client:
URI baseUri = URI.create("http://localhost:8080/siren-plain-ee/resources/"); SirenClient client = Siren.createClient(ClientBuilder.newClient()); Entity entity = client.retrieveEntity(baseUri); entity = client.followLink(entity, "books"); client.performAction(entity, "delete-book");
For a more comprehensive example see both the siren4javaee and siren-client approaches in Hypermedia with JAX-RS.
Inspired by Siren4J.