Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove hypermedia elements from representations produced by Spring Data REST?

When using Spring Data for my REST API, the responses returned currently include the _links field:

{
  "_embedded": {
  "users": [
    {
      "imageUrl": "some_image_url",
      "name": "some name",
      "id": "57420b2a0d31bb6cef4ee8e9",
      "_links": {
        "self": {
          "href": "http://localhost:8080/users/57420b2a0d31bb6cef4ee8e9"
        },
        "user": {
          "href": "http://localhost:8080/users/57420b2a0d31bb6cef4ee8e9{?projection}",
          "templated": true
        }
      }
    },
...

Is there a way to produce output, such that the _links field is hidden? e.g.:

{
  "_embedded": {
  "users": [
    {
      "imageUrl": "some_image_url",
      "name": "some name",
      "id": "57420b2a0d31bb6cef4ee8e9",
    },
...

I find that because I am exposing the id field, _links are not really necessary, and mostly just clutter up my responses.

like image 245
Kevin Grant Avatar asked May 26 '16 18:05

Kevin Grant


People also ask

What does the @RepositoryRestResource annotation do?

The @RepositoryRestResource annotation is optional and is used to customize the REST endpoint. If we decided to omit it, Spring would automatically create an endpoint at “/websiteUsers” instead of “/users“. That's it! We now have a fully-functional REST API.

What is hypermedia in REST API?

Hypermedia is an important aspect of REST. It lets you build services that decouple client and server to a large extent and let them evolve independently. The representations returned for REST resources contain not only data but also links to related resources.

Is used for exposing spring data repositories over REST using Spring data REST?

Spring Data REST can be used to expose HATEOAS RESTful resources around Spring Data repositories. Without writing a lot of code, we can expose RESTful API around Spring Data Repositories.

Why is Spring data REST not recommended in real world applications?

Real-world applications should avoid using Spring Data REST because the entities are exposed as RESTful Services. The two most critical considerations in designing a RESTful service are the domain model and the consumers.


1 Answers

There isn't. Hypermedia is a fundamental trait of REST APIs and Spring Data REST heavily uses it to allow you to build clients that can use the links present in the responses to navigate to related resources.

Of course you can dumb down your clients to not make use of that information but that will lead to a much tighter coupling (as you can't change the URIs on the server side anymore, your clients expects to talk to a dedicated server whereas with hypermedia you can just point it to a different server etc.).

In contrast to a lot of other self-proclaimed REST frameworks out there, one of the key aspects of the framework's design is to respect the fundamental principles in REST and explicitly leverage them. Or at least, don't create incentives to easily break them. This is clearly expressed in the reference documentation and on the project website. Find out more about key design decisions in this presentation on Spring Data REST, and this one on Domain-Driven Design & REST.

like image 183
Oliver Drotbohm Avatar answered Sep 28 '22 00:09

Oliver Drotbohm