在spring数据中,我有一个使用Entity
的EntityStatus
。
当我使用@Controller
序列化json时,id
的EntityStatus
被删除。
这是生成的实体的外观:
{
"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"
}
}
}
我如何强制将其包含在全球所有子实体中?
您可以使用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]));
}
}
如果只希望用于特定实体,请在进行上述处理时将其过滤掉