【问题标题】:Spring Data Rest: removing _links attributes from the response of a hypermedia Rest JPA entitiesSpring Data Rest:从超媒体 Rest JPA 实体的响应中删除 _links 属性
【发布时间】:2017-01-18 20:55:32
【问题描述】:

我想自定义超媒体 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",
    } ]
  }
}

【问题讨论】:

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


【解决方案1】:
@Component
public class MyEntityProcessor implements RepresentationModelProcessor<EntityModel<MyEntity>> {

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

}

【讨论】:

  • 欢迎来到 StackOverflow。虽然此代码可能会回答问题,但提供有关 如何 和/或 为什么 解决问题的附加上下文将提高​​答案的长期价值。
猜你喜欢
  • 2014-10-06
  • 2016-09-24
  • 2019-09-30
  • 1970-01-01
  • 2018-05-10
  • 2014-06-21
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
相关资源
最近更新 更多