26
True RESTful Java Web Services with JSON API and Katharsis MATIJA DUJMOVIĆ (NEOS) ALEKSANDER RADOVAN (KING ICT)

Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Embed Size (px)

Citation preview

Page 1: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

True RESTful Java Web Services with JSON API and KatharsisMATIJA DUJMOVIĆ (NEOS)

ALEKSANDER RADOVAN (KING ICT)

Page 2: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

What is REST?Representational State Transfer

An architectural style defined by Roy Fielding in his doctoral dissertation, described by sixconstraints

Resource-based

Uses representations

Protocol and media type agnostic

Page 3: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Richardson Maturity Model 1/3A convenient model to help explain the specific properties of REST

Divided into four levels

Each higher level represents a more mature REST API with only the last level representing a trueREST API

Page 4: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Richardson Maturity Model 2/3

Page 5: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Richardson Maturity Model 3/3Level 0: Swamp of POX◦ Uses the HTTP protocol just as a transport layer through the HTTP POST method

◦ e.g. SOAP

Level 1: Resources◦ Clearly distinguishes between resources but still uses only the HTTP POST method

◦ e.g. http://example.com/api/projects/123 represents a project resource with id 123

Level 2: HTTP verbs◦ uses all HTTP verbs (GET for reading, POST for creating, PUT or PATCH for updating and DELETE for

deletion)

Level 3: Hypermedia◦ Uses HATEOAS to deal with discovering the possibilities of your API towards the clients

◦ API-s that conform to this level are often called Hypermedia API-s

Page 6: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

HATEOASHypermedia As The Engine Of Application State

A constraint of the REST architectural style

By correctly using a hypermedia type in your REST API you are implicitly using HATEOAS

Let’s explain it by examining an API that uses a hypermedia type!

Page 7: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Hypermedia types 1/2

"Hypermedia Types are MIME media types that contain native hyper-linking semantics that induce application flow. For example, HTML is a hypermedia type; XML is not."

Hypermedia APIs with HTML5 & Node, Mike Amundsen (2010)

"The WWW is fundamentally a distributed hypermedia application.”

Software Architecture: Foundations, Theory and Practice, Taylor, Medividovic, Dashofy (2010)

JSON isn’t a hypermedia type because it doesn’t have built-in support for hyperlinks

The REST architectural style is based on the architecture of the WWW.

Page 8: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Hypermedia types 2/2

Page 9: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

JSON APICreated by Yehuda Katz, started drafting in 2013 and finalized in 2015

JSON API is the only media type that is currently a living standard

Actively used in the Ember.js JavaScript framework

Supported in Java with the Katharsis framework!

Page 10: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Example domain and APIThe API endpoint is http://localhost:8080/api/ , only the changing part of the URI will be shownin examples

The example domain is made of Project and Task objects

A Project object can have many Task objects

A Task object can have only one Project object

Page 11: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

JSON and JSON API comparison

Page 12: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

JSON API related URI

Page 13: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

JSON API include relationship

Page 14: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

JSON API using only one field

Page 15: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

JSON API self URI

Page 16: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

KatharsisThe Katharsis library adds an additional layer on top of RESTful endpoints to provide easyHATEOAS support for Java by implementing the JSON API standard

Standard server-side framework integration◦ Spring Boot

◦ Servlet

◦ JAX-RS (Java API for RESTful Web Services)

◦ Hibernate

Also offers a Java client for JSON API

Page 17: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Spring Boot and Katharsis 1/6Project structure

Page 18: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Spring Boot and Katharsis 2/6application.properties

Page 19: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Spring Boot and Katharsis 3/6ExampleApplication.java

Page 20: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Spring Boot and Katharsis 4/6Project.java , excluded getters, setters and constructors for readability

Page 21: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Spring Boot and Katharsis 5/6ProjectRepository.java

@JsonApiFindOne -> GET projects/123

@JsonApiFindAll -> GET projects

@JsonApiFindAllWithIds -> GET projects/123,124

@JsonApiDelete -> DELETE projects/123

@JsonApiSave -> POST projects , PATCH projects/123

Page 22: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Spring Boot and Katharsis 6/6ProjectToTaskRepository.java , implements the relationship endpoint

e.g.

Page 23: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Alternativeshttp://projects.spring.io/spring-hateoas/ , hypermedia support with link building features

http://elide.io/ , quick JSON API endpoint development for JPA entities

Page 24: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

More informationhttp://amundsen.com/hypermedia/ , Mike Amundsen’s page explaining hypermedia

http://katharsis.io/ , Katharsis project homepage

http://jsonapi.org/ , JSON API homepage

https://gtramontina.github.io/h-factors/ , visualization of available hypermedia and it’s features

Mike Amunden, Build Hypermedia APIs with HTML5 and Node, O’Reilly, 2010

Page 25: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Questions?

Page 26: Javantura v4 - True RESTful Java Web Services with JSON API and Katharsis - Matija Dujmović

Thank you!