Over a million developers have joined DZone.

Introducing Siren 4 Java EE 1.0

When implementing a Hypermedia-driven REST API you may need to choose a content type upfront. Check out how to use the Siren content type together with Java EE.

Build APIs from SQL and NoSQL or Salesforce data sources in seconds. Read the Creating REST APIs white paper, brought to you in partnership with CA Technologies.

When implementing a Hypermedia-driven REST API you may need to choose a content type upfront. If you use the Siren content type together with Java EE there are several approaches to create Siren entity responses.

As your links and actions in your responses may dynamically change depending on your business logic (e.g. some links or actions are only valid in certain situations), it makes sense to use a programmatic rather than declarative approach. Siren4J offers a simple way to both programmatically and declaratively create Siren responses. However this library contains a few third-party dependencies, for instance Jackson.

If you want a leaner approach using plain Java EE 7 you can either use JSON output created by JSONP or a lean library called Siren 4 Java EE. The idea of the latter is to only depend on the Java EE 7 API and therefore use JSONP internally.

Siren 4 Java EE is used as follows:

JsonObject bookEntity = Siren.createEntityBuilder()
 .addProperty("isbn", book.getIsbn())
 .addProperty("name", book.getName())
 .addProperty("author", book.getAuthor())
 .addProperty("availability", book.getAvailability().toString())
 .addProperty("price", book.getPrice())
 .addLink(bookUri, "self")
 .setTitle("Add book to cart")
 .addField("isbn", FieldType.TEXT)
 .addField("quantity", FieldType.NUMBER)).build();

For a more comprehensive example see the siren4javaee approach in Hypermedia with JAX-RS.

The Integration Zone is brought to you in partnership with CA Technologies.  Use CA Live API Creator to quickly create complete application backends, with secure APIs and robust application logic, in an easy to use interface.

java ee ,java ,entity ,introduction ,code

Published at DZone with permission of Sebastian Daschner, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}