10

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",
    } ]
  }
}
Dhruv
  • 10,291
  • 18
  • 77
  • 126
  • 1
    Please, take a look at this http://stackoverflow.com/questions/23264044/disable-hypertext-application-language-hal-in-json – Yuri Sep 11 '16 at 17:58

2 Answers2

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

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

}
jpllosa
  • 2,066
  • 1
  • 28
  • 30
  • 3
    Welcome to StackOverflow. While this code may answer the question, providing additional context regarding *how* and/or *why* it solves the problem would improve the answer's long-term value. – Sven Eberth Jul 10 '21 at 22:36
0

Remove all _links from all objects:

But this method overrides any other processor

@Component
public class ObjectRepresentationModelProcessor implements RepresentationModelProcessor<EntityModel<Object>> {

    @NonNull
    @Override
    public EntityModel<Object> process(EntityModel<Object> model) {
        return EntityModel.of(model.getContent());
    }
}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 22 '23 at 02:47