gpt4 book ai didi

java - Spring Rest数据将一对一关联序列化为相关实体

转载 作者:行者123 更新时间:2023-11-30 06:53:20 25 4
gpt4 key购买 nike

如何配置 Spring Data Rest 来直接序列化相关实体?

我希望它看起来像这样:注意“所有者”链接指向“帐户”实体。

{
"name" : "customer",
"_links" : {
"self" : {
"href" : "http://localhost:8081/api/v1/servers/1005"
},
"owner" : {
"href" : "http://localhost:8081/api/v1/account/100"
}
}
}

当前(默认)具有间接序列化的相关实体(也称为关联)。

我不希望它看起来像这样:“所有者”链接是通过自助服务器实体实现的。

{
"name" : "customer",
"_links" : {
"self" : {
"href" : "http://localhost:8081/api/v1/servers/1005"
},
"owner" : {
"href" : "http://localhost:8081/api/v1/servers/1005/owner"
}
}
}

我检查了文档,没有找到任何提及“直接”路线的内容。

最佳答案

已通过黑客攻击解决。

步骤:

  1. 添加 @RestResource(exported = false)到实体上的关联。
  2. 注册 ResourceProcessor<Resource<OwnedEntity>> @Bean (OwnedEntity 是我的具有所有者的实体的基类)并更改该方法中的链接集合。

详情见Customizing the JSON output section of the Spring Data REST reference docs .

根据请求,这里有一些执行此操作的代码:

/*
* Copyright (c) 2017. DataVolo, Inc. All Rights Reserved.
*/

package com.datavolo.tenant.web;

import com.datavolo.tenant.domain.Account;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.webmvc.support.RepositoryEntityLinks;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.mvc.ResourceAssemblerSupport;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;

/**
*
*/
@Component
public class AccountResourceAssembler extends ResourceAssemblerSupport<Account, AccountResource> {

private final RepositoryEntityLinks repositoryEntityLinks;

@Autowired
public AccountResourceAssembler(@Nonnull RepositoryEntityLinks repositoryEntityLinks) {
super(AccountController.class, AccountResource.class);
this.repositoryEntityLinks = repositoryEntityLinks;
}

@Override
public AccountResource toResource(Account entity) {
Link accountLink = repositoryEntityLinks.linkToSingleResource(Account.class, entity.getId());
String accountHref = accountLink.getHref();
Link selfLink = new Link(accountHref, Link.REL_SELF);

Link subAccounts = new Link(accountHref + "/subAccounts", "subAccounts");
Link owner = new Link(accountHref + "/owner", "owner");

Account parent = entity.getParent();
Link[] links;
if (parent == null) {
links = new Link[] {selfLink, accountLink, subAccounts, owner};
} else {
Link parentAccountLink = repositoryEntityLinks.linkToSingleResource(Account.class, parent.getId());
Link parentLink = new Link(parentAccountLink.getHref(), "parent");
links = new Link[] {selfLink, accountLink, subAccounts, owner, parentLink};
}

return new AccountResource(entity, links);
}
}

然后将其注入(inject) Controller (用 @RepositoryRestController 注释),进而生成响应。

在这个系统中,我们有一个 Controller 的共享基类,并且我们有一个 Multi-Tenancy 设置,其中所有重要的、非查找的域对象(例如,存储系统数据的所有内容)都直接或间接引用帐户对象,控制并代表租赁。因此,我们在一个地方对一个对象执行此操作,然后就完成了。其他链接更加手动,随着时间的推移,我们基本上只是耸耸肩,保留默认的 Spring HATEOS 输出不变,让客户端进行调整。仅当默认情况经常导致到后端的多次往返时,我们才更改此设置 - 这是 Spring 处理它的默认方式的基本问题。但这是一个权衡。当后端资源本身被声明为延迟解析引用时,Spring 的默认设置不会导致额外的开销。对此的一个很好的增强是让它对这些资源更加智能,这样那些已经获取的资源就可以在 REST 响应中通过它们自己的 id 直接引用。

关于java - Spring Rest数据将一对一关联序列化为相关实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42308899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com