Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Rest Without HATEOAS

Tags:

I really like all the boilerplate code Spring Data Rest writes for you, but I'd rather have just a 'regular?' REST server without all the HATEOAS stuff. The main reason is that I use Dojo Toolkit on the client side, and all of its widgets and stores are set up such that the json returned is just a straight array of items, without all the links and things like that. Does anyone know how to configure this with java config so that I get all the mvc code written for me, but without all the HATEOAS stuff?

like image 540
Matt Avatar asked Jun 26 '14 02:06

Matt


People also ask

Is HATEOAS mandatory for REST?

It never has been, it's a much higher level than that. But when you do need REST, and you do use REST, then HATEOAS is a necessity. It's part of the package and a key to what makes it work at all.

Is HATEOAS necessary?

However, HATEOAS compliance is really not necessary for a good API Integration. There are other options out there also and some might argue those to be much better. It all depends on your business needs and strategy.

Why do we need spring HATEOAS?

Spring HATEOAS provides some APIs to ease creating REST representations that follow the HATEOAS principle when working with Spring and especially Spring MVC. The core problem it tries to address is link creation and representation assembly.

Should I use spring HATEOAS?

Spring HATEOAS provides common abstractions (representational models, a Link class, API to build links pointing to Spring MVC controllers, etc.) to ease building hypermedia driven REST APIs with Spring MVC in general. Thus, you can use it alongside Spring MVC to manually build those services.


2 Answers

After reading Oliver's comment (which I agree with) and you still want to remove HATEOAS from spring boot.

Add this above the declaration of the class containing your main method:

@SpringBootApplication(exclude = RepositoryRestMvcAutoConfiguration.class) 

As pointed out by Zack in the comments, you also need to create a controller which exposes the required REST methods (findAll, save, findById, etc).

like image 182
Megadec Avatar answered Nov 14 '22 03:11

Megadec


So you want REST without the things that make up REST? :) I think trying to alter (read: dumb down) a RESTful server to satisfy a poorly designed client library is a bad start to begin with. But here's the rationale for why hypermedia elements are necessary for this kind of tooling (besides the probably familiar general rationale).

Exposing domain objects to the web has always been seen critically by most of the REST community. Mostly for the reason that the boundaries of a domain object are not necessarily the boundaries you want to give your resources. However, frameworks providing scaffolding functionality (Rails, Grails etc.) have become hugely popular in the last couple of years. So Spring Data REST is trying to address that space but at the same time be a good citizen in terms of restfulness.

So if you start with a plain data model in the first place (objects without to many relationships), only want to read them, there's in fact no need for something like Spring Data REST. The Spring controller you need to write is roughly 10 lines of code on top of a Spring Data repository. When things get more challenging the story gets becomes more intersting:

  • How do you write a client without hard coding URIs (if it did, it wasn't particularly restful)?
  • How do you handle relationships between resources? How do you let clients create them, update them etc.?
  • How does the client discover which query resources are available? How does it find out about the parameters to pass etc.?

If your answers to these questions is: "My client doesn't need that / is not capable of doing that.", then Spring Data REST is probably the wrong library to begin with. What you're basically building is JSON over HTTP, but nothing really restful then. This is totally fine if it serves your purpose, but shoehorning a library with clear design constraints into something arbitrary different (albeit apparently similar) that effectively wants to ignore exactly these design aspects is the wrong approach in the first place.

like image 23
Oliver Drotbohm Avatar answered Nov 14 '22 04:11

Oliver Drotbohm