我正在使用spring-data-rest将实体作为(分页)rest资源公开。一切都运行良好,但是当我通过RestTemplate
请求数据时,我会得到一个无用的HATEOAS JSON(我没有要求)。JSON似乎是一个PagedResources。我可以接受这点,但JSON没有正确地转换成一个对象。里面没有content
。
仓库:
@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
}
System.out:
0 // getContent().size()
4 // getLinks().size()
2 // getTotalElements()
curl:
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
没有正确映射到内容?!
application/hal+json
。通常这种负面语气不会激励人回答问题。尽管如此,我还是花了时间回答你。不客气。顺便问一句,什么是HATEOAS JSON? - Oliver Drotbohm