Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Rest: removing _links attributes from the response of a hypermedia Rest JPA entities

I want to customize the response of a hypermedia Rest JPA entity and want to remove all the _links attributes and self link attributes. My client application is not that big and it knows what exact REST API's to call. So I feel these extra bytes travelling in HTTP packet would always be a over head in my application.

So how can I achieve to remove this links attributes from the response?

Right now REST API response is:

{
  "_embedded" : {
    "questionsTypes" : [ {
      "queTypeID" : 2,
      "queDescription" : "Single choice rating selection",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/question_web/rest/QuestionsType/2"
        },
        "questionsType" : {
          "href" : "http://localhost:8080/question_web/rest/QuestionsType/2{?projection}",
          "templated" : true
        }
      }
    },{
      "queTypeID" : 5,
      "queDescription" : "Subjective questions",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/question_web/rest/QuestionsType/5"
        },
        "questionsType" : {
          "href" : "http://localhost:8080/question_web/rest/QuestionsType/5{?projection}",
          "templated" : true
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/question_web/rest/QuestionsType"
    },
    "profile" : {
      "href" : "http://localhost:8080/question_web/rest/profile/QuestionsType"
    },
    "search" : {
      "href" : "http://localhost:8080/question_web/rest/QuestionsType/search"
    }
  }
}

Final response I expect, is something like this:

{
  "_embedded" : {
    "questionsTypes" : [ {
      "queTypeID" : 2,
      "queDescription" : "Single choice rating selection",
    },{
      "queTypeID" : 5,
      "queDescription" : "Subjective questions",
    } ]
  }
}
like image 971
Dhruv Bansal Avatar asked Sep 11 '16 05:09

Dhruv Bansal


1 Answers

@Component
public class MyEntityProcessor implements RepresentationModelProcessor<EntityModel<MyEntity>> {

    @Override
    public EntityModel<MyEntity> process(EntityModel<MyEntity> model) {
        return EntityModel.of(model.getContent());
    }

}
like image 191
user16392372 Avatar answered Nov 15 '22 05:11

user16392372