[SPRING] RestTemplate이 PagedResources에 응답 표현을 바인드하지 않는 이유는 무엇입니까?
SPRINGRestTemplate이 PagedResources에 응답 표현을 바인드하지 않는 이유는 무엇입니까?
엔티티를 (호출 된) 나머지 리소스로 노출시키기 위해 spring-data-rest를 사용하고 있습니다. 모든 것이 잘 작동하지만 RestTemplate을 통해 데이터를 요청할 때 쓸모없는 HATEOAS JSON이 생깁니다. JSON은 PagedResources 인 것 같습니다. 그것으로 살 수는 있지만 JSON은 객체로 올바르게 변환되지 않습니다. 내부에 내용이 없습니다.
저장소:
@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()
컬:
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가 콘텐츠에 올바르게 매핑되지 않은 것 같습니다.
해결법
-
==============================
1.올바르게 발견 했으므로 PagedResources에는 _embedded 속성이 없으므로 콘텐츠 속성이 채워지지 않습니다.
올바르게 발견 했으므로 PagedResources에는 _embedded 속성이 없으므로 콘텐츠 속성이 채워지지 않습니다.
이 딜레마는 두 가지 방법으로 해결할 수 있습니다.
from https://stackoverflow.com/questions/23239052/why-does-resttemplate-not-bind-response-representation-to-pagedresources by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] 액세스 토큰을 발급 할 때 사용자 정보를 포함 할 수 있습니까? (0) | 2018.12.17 |
---|---|
[SPRING] TaskExecutor 예제를 사용한 좋은 스프링 스레딩? [닫은] (0) | 2018.12.17 |
[SPRING] 영구 저장소에서 예외로드 세션 (0) | 2018.12.17 |
[SPRING] 스프링 배치 (Spring Batch)의 ItemReader에서 작업 매개 변수에 액세스하는 방법 (0) | 2018.12.17 |
[SPRING] mvc : resources - Spring의 주석 구성 대체 (0) | 2018.12.17 |