Spring如何在@Controller生成的json中保留所有子实体的实体ID?

问题描述 投票:0回答:1

在spring数据中,我有一个使用EntityEntityStatus

当我使用@Controller序列化json时,idEntityStatus被删除。

这是生成的实体的外观:

{
  "description" : "Some testing",
  "version" : null,
  "createdDate" : "2020-03-10T05:46:25.516Z",
  "createdById" : null,
  "lastModifiedById" : null,
  "title" : "This is a test",
  "content" : "Foo bar fizz buzz",
  "userId" : 2,
  "category" : {
    "description" : "Foobar",
    "version" : null,
    "createdDate" : "2020-03-10T05:18:30.282Z",
    "lastModifiedDate" : "2020-03-10T05:18:41.827Z",
    "createdById" : null,
    "lastModifiedById" : null,
    "deleted" : false
  },
  "status" : {
    "description" : "Created"
  },
  "deleted" : false,
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/management/entity/5"
    }
  }
}

我如何强制将其包含在全球所有子实体中?

java spring spring-boot spring-data spring-data-rest
1个回答
0
投票

您可以使用RepositoryRestConfigurerAdapter进行配置。如果您想为所有实体使用它,只需遍历所有实体并公开其ID:

@Configuration
public class MyRepositoryRestConfigurerAdapter extends RepositoryRestConfigurerAdapter {

    @Autowired
    private EntityManager entityManager;

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(entityManager.getMetamodel().getEntities()
              .stream().map(EntityType:getJavaType))
              .collect(Collectors.toList())
              .toArray(new Class[0]));
    }

}

如果只希望用于特定实体,请在进行上述处理时将其过滤掉

© www.soinside.com 2019 - 2024. All rights reserved.