gpt4 book ai didi

Spring Data REST 返回 EmptyCollectionEmbeddedWrapper 而不是空集合

转载 作者:行者123 更新时间:2023-12-03 19:37:55 25 4
gpt4 key购买 nike

我正在开发基于 Spring Data REST 的服务。由于我们使用 swagger(通过 SpringFox 生成)创建前端代码这一事实,我不得不停用 HAL 格式的返回,该格式在一个异常(exception)情况下工作正常。

如果请求的结果是一个空列表,则响应如下所示

{
"links": [
{
"rel": "self",
"href": "http://localhost:9999/users"
},
{
"rel": "profile",
"href": "http://localhost:9999/profile/users"
}
],
"content": [
{
"rel": null,
"collectionValue": true,
"relTargetType": "com.example.User",
"value": []
}
]
}

如何获取空列表作为内容?

最佳答案

我不得不调整上一个解决方案中提供的解决方案,以使用 Spring HATEOAS 1.x 引入的类型
这是我正在使用的代码:

@Component
public class ResourceProcessorEmpty implements RepresentationModelProcessor<CollectionModel<Object>> {
@Override
public CollectionModel<Object> process(CollectionModel<Object> resourceToThrowAway) {
if (resourceToThrowAway.getContent().size() != 1) {
return resourceToThrowAway;
}
if (!resourceToThrowAway.getContent().iterator().next().getClass().getCanonicalName().contains("EmptyCollectionEmbeddedWrapper")) {
return resourceToThrowAway;
}

CollectionModel<Object> newResource = new CollectionModel<>(Collections.emptyList());
newResource.add(resourceToThrowAway.getLinks());
return newResource;
}
}

关于Spring Data REST 返回 EmptyCollectionEmbeddedWrapper 而不是空集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45590207/

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