Spring Data Rest:从超媒体Rest JPA实体的响应中删除_links属性

时间:2016-09-11 05:03:50

标签: spring spring-data-rest spring-hateoas

我想自定义超媒体Rest JPA实体的响应,并希望删除所有_links属性和自链接属性。 我的客户端应用程序不是那么大,它知道要调用哪些确切的REST API。因此,我觉得在HTTP数据包中传输的这些额外字节在我的应用程序中始终是一个空头。

那么如何从响应中删除此链接属性呢?

目前REST API响应是:

{
  "_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"
    }
  }
}

我期待的最终回应是这样的:

{
  "_embedded" : {
    "questionsTypes" : [ {
      "queTypeID" : 2,
      "queDescription" : "Single choice rating selection",
    },{
      "queTypeID" : 5,
      "queDescription" : "Subjective questions",
    } ]
  }
}

1 个答案:

答案 0 :(得分:1)

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

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

}