为什么 RestTemplate 不将响应表示绑定到 PagedResources? [英] Why does RestTemplate not bind response representation to PagedResources?

查看:31
本文介绍了为什么 RestTemplate 不将响应表示绑定到 PagedResources?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring-data-rest 将实体公开为(分页的)rest 资源.一切正常,但是当我通过 RestTemplate 请求数据时,我得到了一个无用的 HATEOAS JSON(我没有要求).JSON 似乎是一个 PagedResources.我可以接受,但是 JSON 没有正确转换为对象.里面没有content.

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 HATEOAS JSON (which I didn't ask for). The JSON seems to be a PagedResources. I could live with that, but the JSON isn't converted into an object correctly. There is no content inside.

存储库:

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long>
{
    List<Person> findByLastName(@Param("name") String name);
}

客户:

public List<Person> getPersons()
{
    RestTemplate rt = new RestTemplate();
    System.out.println(rt.getForObject(URL, PagedResources.class).getContent().size());
    System.out.println(rt.getForObject(URL, PagedResources.class).getLinks().size());
    System.out.println(rt.getForObject(URL, PagedResources.class).getMetadata().getTotalElements());
    return new ArrayList<Person>(rt.getForObject(URL, PagedResources.class).getContent()); // <-- empty
}

系统输出:

0 // getContent().size()
4 // getLinks().size()
2 // getTotalElements()

卷曲:

C:...>curl http://localhost:8080/spring-jsf-rest/rest/people
{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/spring-jsf-rest/rest/people{?page,size,sort}",
      "templated" : true
    },
    "search" : {
      "href" : "http://localhost:8080/spring-jsf-rest/rest/people/search"
    }
  },
  "_embedded" : {
    "people" : [ {
      "firstName" : "John",
      "lastName" : "Rambo",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/spring-jsf-rest/rest/people/1"
        }
      }
    }, {
      "firstName" : "Chuck",
      "lastName" : "Norris",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/spring-jsf-rest/rest/people/2"
        }
      }
    } ]
  },
  "page" : {
    "size" : 20,
    "totalElements" : 2,
    "totalPages" : 1,
    "number" : 0
  }
}

似乎 _embedded 没有正确映射到内容?!

It seems like _embedded is not mapped correctly to content?!

推荐答案

正如你所发现的,PagedResources 没有 _embedded 属性,这就是为什么你没有不填充 content 属性.

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. 首先提供与表示相匹配的类型.因此,创建一个自定义类并坚持表示的属性名称或使用 Jackson 注释等对其进行自定义.

  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.

设置自定义 MappingJackson2HttpMessageConverter 并自定义 ObjectMapper 以获取 Spring HATEOAS 开箱即用的 Jackson2HalModule 配置.

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));

这篇关于为什么 RestTemplate 不将响应表示绑定到 PagedResources?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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