简体   繁体   English

如何使用Spring Data Rest在GET调用中获取自引用对象

[英]How to fetch Self Referencing object in GET call using Spring Data Rest

I am using Spring 1.3.3 and I am unable to get Self Referencing object in the Spring Data Rest Response using GET even if it is not null. 我正在使用Spring 1.3.3,并且即使它不为null,也无法使用GET在Spring Data Rest Response中获取自引用对象。

eg 例如

My Table: 我的桌子:

CREATE TABLE `employee` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(40) NOT NULL,
  `parent_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`),
  KEY `FK_employee_parent` (`parent_id`),
  CONSTRAINT `FK_employee_parent` FOREIGN KEY (`parent_id`) REFERENCES `employee` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8057 DEFAULT CHARSET=latin1

Response: 响应:

{
"id": 1,
"name": "Test Employee",
"_links": {
"self": {
"href": "http://localhost:8081/employee/1"
},
"employee": {
"href": "http://localhost:8081/employee/1"
},
"parent": {
"href": "http://localhost:8081/employee/1/parent"
}
}
}

But I need the parent_id next to the name field instead of under " links ". 但是我需要名称字段旁边的parent_id,而不是在“ links ”下。

  1. Is there a way to return the parentId in the Employee object (next to the name) ? 有没有一种方法可以在Employee对象(名称旁边)中返回parentId?

OR 要么

  1. Should I need to add the projection to return the self referencing object? 我是否需要添加投影以返回自引用对象?

I would suggest to use jackson on top of your spring rest. 我建议在春季休息时使用杰克逊。 You can then easily add an annotation to the domain object to rename links to parent_id. 然后,您可以轻松地将注释添加到域对象,以重命名指向parent_id的链接。

 @JsonProperty("parent_id")

You also need to add two annotations above the class 您还需要在类上方添加两个注释

@JsonSerialize
@JsonInclude

Best practice actually is not to use the domain object directly but to use a pojo in between which is dealing with this. 最佳实践实际上不是直接使用域对象,而是在两者之间使用pojo处理该对象。 So then the domain object data will be copied to that pojo and you will only show what you want to show in the rest response. 因此,然后将域对象数据复制到该pojo,您将仅在其余响应中显示要显示的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Spring Data Rest在GET调用中获取EmbeddedId对象 - How to fetch embeddedId object in GET call using Spring Data Rest 如何使用Spring Data Rest在POST调用中保存嵌入式对象 - How to save embedded object in POST call using Spring Data Rest 如何使用spring rest api从休眠的两个表中获取数据并在单个对象中返回URL - How to fetch data from two tables in hibernate with spring rest api and return in single object to url 如何使用Spring Data REST在OneToMany关系中添加对象 - How to add an object in a OneToMany relationship using Spring Data REST 如何通过自链接在 Spring Data REST 服务器中加载实体? - How to Load an Entity Inside a Spring Data REST Server by Self Link? 如何通过Rest调用使用输入表单获取数据? - How to get data using an input form through a rest call? 使用 Spring-Data-Rest,如何从一个 REST 调用更新 OneToOne 关系双方的 FK? - Using Spring-Data-Rest, how to update FK on both sides of OneToOne relationship from one REST call? 如何在 spring 中使用 Rest API 从多个表中获取数据 - how to fetch data using Rest API in spring boot from multiple tables 如何使用Spring Rest调用URL? - How to call a URL using spring rest? Spring Data REST调用以空变量保存对象 - Spring Data REST call to `save` receiving object with null variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM