GithubHelp home page GithubHelp logo

restful-rental-store's Introduction

A RESTful Video Rental Store API

Description

This is an implementation of a Video Rental Store RESTful API. It is a level 3 RESTful API according to Richardson's maturity levels 1. Put simply, it uses Hypermedia As The Engine Of Application State, a.k.a HATEOAS.

The project is built using Spring Boot, and particularly Spring Data, Spring MVC and Spring HATEOAS. All resource representation is based on HAL 2 which is nicely supported by Spring HATEOAS. All project layout and design adheres to Domain Driven Design principles 3. I have tried to make the project as conformant to DDD as possible and avoid the dreadful Anemic Domain Model antipattern 4. However it is still not fully DDD, since for the sake of simplicity I let the JPA Entity annotations bleed into the Domain classes.

Hypermedia and Profiles

HAL is a rather lightweight hypermedia format. At least as long as the hypermedia controls is concerned. With HAL, you can't really express whether a client should use POST, PUT or DELETE with a link representation. Other hypermedia formats, like Siren are more advanced in this regard, and allow you to express actions, which contain both the HTTP verb and request body to send to the endpoint. This is great since it enables you to provide a fully self contained representation of a resource, and almost completely avoid external documentation. A representation containing all possible next actions and the way to proceed with them.

As of this writing there is no Siren support in Spring, so I am stuck with HAL. There is a new project in Spring though, called Spring Affordances, which adds support for a HAL extension called HAL-FORMS 5. This extension provides support for the self contained resource representations described before. Regretfully the project isn't publicly available yet.

In order to try to fill the semantic gap left by HAL, some external documentation is required. One with a proper description of the link relations, what they mean, the HTTP verb that should be used, and a nice resource representation description. Some like to use Swagger to provide this documentation, but I feel it is more REST-level-2 oriented. Others like to provide the documentation in plain english. I think it is much better to provide a machine-readable documentation. One that we can use to build smart clients. Basically, this documentation is often called a Profile. A document that provides additional semantic information on top of a Media Type without altering it 6. There are several Profile formats available. XMDP for instance, which is suitable for microformat based media types. I will be looking at ALPS, which is JSON based and can be used later on with any Hypermedia Type of choice.

A look at ALPS

Spring Data REST supports ALPS out of the box. Nevertheless I am not using Spring Data REST, but Spring HATEOAS. Spring Data REST adds support to export Spring Data Repositories directly. Sparing you from the creation of your own Spring MVC Controllers and Resources. As useful and convenient as Spring Data REST is, I think it doesn't help much our DDD goals. In my humble opinion your Entity classes often end up having representation annotations, and I think it is better to have the Domain Model as cleanly segregated from the infrastructure as possible. Thus, I am not the biggest fan of adding even more annotations to the Domain Model classes. Remember they are already polluted with Java Persistence Annotations. Something I am not quite happy about.

Spring HATEOAS comes with several ALPS support classes. However, it lacks any kind of profile controller, and all the logic provided by Spring Data REST. You could think of using just Spring Data REST's ALPS support, and sticking to Spring HATEOAS for the rest. I have given it a try, but Spring Data REST's controllers are too tightly coupled to the Spring Data Repositories and PersistentEntity classes. The whole idea of exposing Repositories as RESTful resources seems to be deeply ingrained in Spring Data REST. You can have a look at the class org/springframework/data/rest/webmvc/ProfileController.java for more information. I would say that the ALPS profile implementation in Spring Data doesn't lend itself too much for reuse in other RESTful projects that are not Repository based. Or whose Resources are not coming directly from a Spring Data Repository.

A final idea worth exploring could be the creation of my own ProfileController, which would serve the final ALPS document. In order to make things easier, I think I could maybe reuse the ResourceAssemblers and add the corresponding link to the ALP section for that resource. There should also be something in the custom ProfileController that was able to discover the resources that would be served by the different controllers, and also to translate those resources into their corresponding ALPS representation.

References

restful-rental-store's People

Contributors

jisfy avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.