Spring Data REST - Consume List of Entities, Java HATEOAS client

╄→尐↘猪︶ㄣ 提交于 2019-11-28 04:58:25

问题


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

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
        )

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

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
            )

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=[...]}>

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
 }
}

Repository Definition:

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

Any thoughts on what I am missing?


回答1:


I solved this by doing a few things.

  1. I had to update to spring-hateoas:0.20.0.RELEASE from 0.19.0. spring-hateoas:0.19.0 did not support jackson 2.7+ as specified here.

  2. 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 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's comment got me looking more closely at the PagedResources implementation, which eventually led to the 'aha!' moment with this blog.

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.




回答2:


Here is my solution:

    ParameterizedTypeReference<PagedResources<EntityObject>> responseType;
    responseType = new ParameterizedTypeReference<PagedResources<EntityObject>>() { };

    ResponseEntity<PagedResources<EntityObject>> pageResources;
    pageResources = restTemplate
            .withBasicAuth(username, password)
            .exchange(
                    UriComponentsBuilder.fromPath("/your/api/path").build().toString(),
                    GET,
                    null,
                    responseType
            );

    PagedResources<EntityObject> resource = pageResources.getBody();
    assertTrue(resource.getContent().isEmpty());


来源:https://stackoverflow.com/questions/38468839/spring-data-rest-consume-list-of-entities-java-hateoas-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!