Why does RestTemplate not bind response representation to PagedResources?

前端 未结 1 688
清酒与你
清酒与你 2020-11-29 22:04

I am using spring-data-rest to expose entities as (paged) rest resources. Everything works fine, but when I request data via RestTemplate, I get an useless HATE

相关标签:
1条回答
  • 2020-11-29 22:27

    As you've discovered correctly, PagedResources does not have an _embedded property, that's why you don't get the content property populated.

    This dilemma can be solved in two different ways:

    1. Providing a type that matches the representation in the first place. Thus, craft a custom class and either stick to the property names of the representation or customize it using Jackson annotations etc.

    2. Set up a custom MappingJackson2HttpMessageConverter and customize the ObjectMapperto get the Jackson2HalModule configured that Spring HATEOAS ships out of the box.

      ObjectMapper mapper = new ObjectMapper();
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      mapper.registerModule(new Jackson2HalModule());
      
      MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
      converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
      converter.setObjectMapper(mapper);
      
      RestTemplate template = new RestTemplate(Collections.<HttpMessageConverter<?>> singletonList(converter));
      
    0 讨论(0)
提交回复
热议问题