Spring Data REST - 使用实体列表,Java HATEOAS 客户端 [英] Spring Data REST - Consume List of Entities, Java HATEOAS client

查看:34
本文介绍了Spring Data REST - 使用实体列表,Java HATEOAS 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Spring Data REST 2.5.1、Jackson 2.8.0、Spring Boot 1.3.6.

I'm using Spring Data REST 2.5.1, Jackson 2.8.0, Spring Boot 1.3.6.

我正在尝试通过 RestTemplate 从我的存储库中检索一个简单的实体列表.我可以在浏览器中到达终点,并获得预期的 HAL 数据.检索单个实体工作正常,如下所示.这些都使用默认的 SDR 端点(例如 localhost:{port}/myEntity).

I'm trying to retrieve a simple list of entities from my Repository via RestTemplate. I can hit the end point in a browser, and get the expected HAL data. Retrieving a single Entity works fine as below. These are all using the default SDR endpoints (e.g. localhost:{port}/myEntity).

    ResponseEntity<Resource<MyEntity>> responseEntity =
    new RestTemplate()
        .exchange(
        uri + "/1",
            HttpMethod.GET,
            HttpEntity.EMPTY,
        new ParameterizedTypeReference<Resource<MyEntity>>() {}, port
        )

或者new RestTemplate().getForEntity(uri + "/1", MyEntity.class, port)

Or new RestTemplate().getForEntity(uri + "/1", MyEntity.class, port)

正如许多 SO 问题似乎表明的那样,查找检索列表的示例是一个问题.我已经尝试了 ParameterizedTypeReferenceResourcesResourceMyEntity、一个数组、List.一切都没有运气.

As a lot of SO questions seem to indicate, finding examples of retrieving a list is a problem. I've tried the ParameterizedTypeReference with Resources,Resource, MyEntity, an array, List. All with no luck.

        ResponseEntity<Resources<Resource<MyEntity>>> responseEntity =
            new RestTemplate()
                    .exchange(
                    uri,
                    HttpMethod.GET,
                    HttpEntity.EMPTY,
                    new ParameterizedTypeReference<Resources<Resource<MyEntity>>>() {}
            , port
            )

当使用几乎任何种类的 ResourcesResourceListMyEntity 像上面一样调用时> 等,ResponseEntity 为空.喜欢:

When called like above with pretty much any variety of Resources, Resource, List<MyEntity>, MyEntity, etc., the ResponseEntity is empty. Like:

<200 OK,Resources { content: [], links: [] },{Server=[Apache-Coyote/1.1], Content-Type=[application/json;charset=UTF-8], Transfer-Encoding=[chunked], Date=[...]}>

字符串 JSON 在浏览器中如下所示.

The string JSON looks like below in browser.

{
"_embedded" : {
"myEntities" : [ ... ]
},
"_links" : {
"self" : {
  "href" : "http://localhost:8080/myEntity"
},
"profile" : {
  "href" : "http://localhost:8080/profile/myEntity"
},
"search" : {
  "href" : "http://localhost:8080/myEntity/search"
}
},
"page" : {
  "size" : 20,
  "totalElements" : 10,
  "totalPages" : 1,
  "number" : 1
 }
}

存储库定义:

@RepositoryRestResource(collectionResourceRel = "myEntities", path = "myEntity")
public interface MyEntityRepository extends PagingAndSortingRepository<MyEntity, Long>
, QueryDslPredicateExecutor<MyEntity>
, QuerydslBinderCustomizer<QMyEntity> { }

对我缺少的东西有什么想法吗?

Any thoughts on what I am missing?

推荐答案

我做了一些事情解决了这个问题.

I solved this by doing a few things.

  1. 我不得不从 0.19.0 更新到 spring-hateoas:0.20.0.RELEASE.spring-hateoas:0.19.0 不支持 jackson 2.7+ 指定此处.

我更新了我的客户以调用如下.

I updated my Client to call as below.

ObjectMapper mapper = builder.build()
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();

messageConverter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"))
messageConverter.setObjectMapper(mapper)

ResponseEntity<PagedResources<MyEntity>> responseEntity =
        new RestTemplate(Arrays.asList(messageConverter))
                .exchange(
                uri,
                HttpMethod.GET,
                HttpEntity.EMPTY,
                new ParameterizedTypeReference<PagedResources<MyEntity>>() {}, port
        )

PagedResources 现在看起来像这样:

PagedResources now looks like this:

<200 OK,PagedResource { content: [{<List of MyEntities>}], metadata: Metadata { number: 0, total pages: 1, total elements: 10, size: 20 }, links: [<List of hateoas links for MyEntities>] },{Server=[Apache-Coyote/1.1], Content-Type=[application/hal+json;charset=UTF-8], Transfer-Encoding=[chunked], Date=[Thu, 21 Jul 2016 14:57:18 GMT]}>

@zeroflagL 的评论让我更仔细地研究了 PagedResources 实现,这最终导致了啊哈!"这个博客.

@zeroflagL's comment got me looking more closely at the PagedResources implementation, which eventually led to the 'aha!' moment with this blog.

相关的一点是默认的RestTemplate 没有将接受头设置为application/hal+json.相反,默认值是 application/x-spring-data-compact+json;charset=UTF-8 ,它没有内容,只有链接.这就是为什么我的 Resources 类型得到空内容的原因.如上所述显式设置 MediaType 解决了该问题.

The relevant bit is that the default RestTemplate doesn't set the accept header to application/hal+json. Instead, the default is application/x-spring-data-compact+json;charset=UTF-8 which has not content and only links. This is why I was getting empty content for my Resources types. Explicitly setting the MediaType as above fixed the issue.

这篇关于Spring Data REST - 使用实体列表,Java HATEOAS 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆