Last week I blogged about doing a RESTful Web Service in Spring. Towards the end of that post I mentioned that there's another module in Spring Data which can automate some of this work called Spring Data REST.
Following through on that idea, I rewrote essentially the same service here using Spring Data REST.
From what I've seen so far, Spring Data REST helps you in two different ways. It relieves you of having to write a controller, at least for basic CRUD methods, and it also enables Hypermedia As The Engine Of Application State (HATEOAS).
When I started trying to get my Spring MVC Test Framework integration tests working again, the first thing I noticed was the following test failing:
My test was expecting application/json but it's now returning the HATEAOS mime type application/hal+json.
Spring defines these in the MediaType class. They don't seem to have one of those for application/hal+json. I considered just not checking the mime type... But I googled a bit more and found Configuring Spring Data REST and Basic settings for Spring Data REST in the documentation. I used the following configuration to change the default mime type of the JSON responses back to application/json:
The next thing that was different has to do with the argument to methods which operate on an existing entity. My original code used a String "name" to identify the entity, but the Spring Data REST methods all use the "id" field. Here's how I wrote the controller method to get a single entity by name:
But I'm not writing the controllers any more... Spring Data Rest creates a 'GET' endpoint taking an 'id' value, and it also creates one for my findByName() method in the Spring Data JPA repository interface. Here's my tests for these two cases:
I rewrote the remaining tests to just use the id instead of the name.
One other change was needed... since Spring is now automatically generating a REST endpoint from the findByName() method in the repository interface, instead of me just calling it from the controller method, I needed to give that an annotation to instruct it how to handle the HTTP request parameter as its input:
Spring Data REST strikes me as a huge win for green field development situations where you just drop a dependency in the POM file (or Gradle build file) and, whoosh, instant REST endpoints for all the domain classes. It might be more problematic to use this in a legacy app where the Spring Data REST endpoints might not match the conventions of the existing endpoints. Also, the HATEOAS stuff, yeah, it's the semantic web wave of the future, but it is a bit different and might take some getting used to if this is your first exposure to it.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment