我使用spring-data-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);
}
Run Code Online (Sandbox Code Playgroud)
客户:
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
}
Run Code Online (Sandbox Code Playgroud)
System.out的:
0 // getContent().size()
4 // getLinks().size()
2 // getTotalElements()
Run Code Online (Sandbox Code Playgroud)
卷曲:
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" …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring Data REST 2.5.1,Jackson 2.8.0,Spring Boot 1.3.6.
我正试图通过RestTemplate从我的存储库中检索一个简单的实体列表.我可以在浏览器中找到终点,并获得预期的HAL数据.检索单个实体可以正常工作,如下所示.这些都使用默认的SDR端点(例如localhost:{port}/myEntity).
ResponseEntity<Resource<MyEntity>> responseEntity =
new RestTemplate()
.exchange(
uri + "/1",
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<Resource<MyEntity>>() {}, port
)
Run Code Online (Sandbox Code Playgroud)
或者新的RestTemplate().getForEntity(uri +"/ 1",MyEntity.class,port)
正如许多SO问题似乎表明的那样,找到检索列表的示例是一个问题.我试着ParameterizedTypeReference用Resources,Resource,MyEntity,数组列表.一切都没有运气.
ResponseEntity<Resources<Resource<MyEntity>>> responseEntity =
new RestTemplate()
.exchange(
uri,
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<Resources<Resource<MyEntity>>>() {}
, port
)
Run Code Online (Sandbox Code Playgroud)
当所谓的像上面几乎任何种类的Resources,Resource,List<MyEntity>,MyEntity,等等,ResponseEntity是空的.喜欢:
<200 OK,Resources { content: [], links: [] },{Server=[Apache-Coyote/1.1], Content-Type=[application/json;charset=UTF-8], Transfer-Encoding=[chunked], Date=[...]}>
Run Code Online (Sandbox Code Playgroud)
字符串JSON在浏览器中如下所示.
{
"_embedded" : {
"myEntities" : …Run Code Online (Sandbox Code Playgroud)