【问题标题】:Spring Data REST returns EmptyCollectionEmbeddedWrapper instead of empty collectionSpring Data REST 返回 EmptyCollectionEmbeddedWrapper 而不是空集合
【发布时间】:2018-01-17 07:34:24
【问题描述】:

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

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

{
"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": []
    }
]
}

我怎样才能得到一个空列表作为内容?

【问题讨论】:

  • 嘿@stoetti,你找到解决方案了吗?我也面临同样的问题。
  • 您好!对不起,我没有得到任何答案或找到服务器端解决方案,所以我们在客户端处理了对空响应的特殊处理。
  • 哈,这也是我正在做的。我并没有真正理解这种行为,因为当 get / 没有返回任何结果并且没有任何内容可以返回时,它只会返回一个空的内容数组 []。对我来说,这确实是一个错误,因为它会导致不必要的缺乏一致性。感谢您在此@stoetti 上回复我 ;-)
  • 嘿,有什么发现吗?
  • 没有,但我们没有进一步调查,因为默认的 HATEOAS 序列化响应是空的“嵌入式”集合

标签: spring spring-data-rest spring-hateoas


【解决方案1】:

到目前为止,我最终找到的最佳和最简单的解决方案如下。实现一个自定义的 ResourceProcessor,它会被 spring 自动拾取和使用(因为 @Component)。覆盖 process 方法并在方法中返回一个新的 Resource() ,它用一个空列表初始化,而不是你作为参数得到的旧 Resource,添加链接和所有你想要的东西,就是这样。像这样:

import java.util.Collections;
import javax.servlet.http.HttpServletRequest;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.ResourceProcessor;
import org.springframework.hateoas.Resources;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
public class ResourceProcessorEmpty implements ResourceProcessor<Resources<Object>>
{
    @Override
    public Resources<Object> process(final Resources<Object> resourceToThrowAway)
    {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

// In my case i needed the request link with parameters, and the empty content[] array

        Link link = new Link(request.getRequestURL().toString() + "?" + request.getQueryString());
        Resources<Object> newResource = new Resources<>(Collections.emptyList());
        newResource.add(link);
        return newResource;
    }
}

澄清:如果您使用Resources&lt;Object&gt;,它将处理空集合(当返回“EmptyCollectionEmbeddedWrapper”虚拟对象时),而Resources&lt;Resource&lt;Object&gt;&gt; 将处理非空集合。在这种情况下,需要使用第一个。

【讨论】:

    【解决方案2】:

    我不得不调整之前解决方案中提供的解决方案以使用 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-08
      • 2014-03-18
      • 2020-06-29
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 2014-03-28
      • 1970-01-01
      相关资源
      最近更新 更多