스프링 데이터 REST 2.5.1, Jackson 2.8.0, 스프링 부트 1.3.6을 사용하고 있습니다.스프링 데이터 REST - 엔티티 목록, Java HATEOAS 클라이언트 사용
RestTemplate을 통해 리포지토리에서 간단한 엔티티 목록을 가져 오려고합니다. 나는 브라우저에서 끝점에 도달 할 수 있고 예상되는 HAL 데이터를 얻을 수 있습니다. 단일 엔티티를 검색하는 것은 다음과 같이 잘 동작합니다. 이들은 모두 기본 SDR 엔드 포인트 (예 : localhost : {port}/myEntity)를 사용합니다.
ResponseEntity<Resource<MyEntity>> responseEntity =
new RestTemplate()
.exchange(
uri + "/1",
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<Resource<MyEntity>>() {}, port
)
또는 새로운 RestTemplate(). getForEntity (URI + "/ 1", MyEntity.class, 포트)의 많은으로
는 SO 질문 목록을 가져 오는 중입니다의 예를 찾아, 표시하는 것 문제. 나는 ParameterizedTypeReference
을 Resources
, Resource
, MyEntity
, 배열 List로 시도했다. 모두 운이 없다.
ResponseEntity<Resources<Resource<MyEntity>>> responseEntity =
new RestTemplate()
.exchange(
uri,
HttpMethod.GET,
HttpEntity.EMPTY,
new ParameterizedTypeReference<Resources<Resource<MyEntity>>>() {}
, port
)
Resources
이 Resource
는, List<MyEntity>
는, MyEntity
는 등의 ResponseEntity
가 비어의 거의 모든 다양한 위와 같이 호출합니다. Like :
<200 OK,Resources { content: [], links: [] },{Server=[Apache-Coyote/1.1], Content-Type=[application/json;charset=UTF-8], Transfer-Encoding=[chunked], Date=[...]}>
문자열 JSON은 브라우저에서 다음과 같이 표시됩니다.
{
"_embedded" : {
"myEntities" : [ ... ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/myEntity"
},
"profile" : {
"href" : "http://localhost:8080/profile/myEntity"
},
"search" : {
"href" : "http://localhost:8080/myEntity/search"
}
},
"page" : {
"size" : 20,
"totalElements" : 10,
"totalPages" : 1,
"number" : 1
}
}
저장소 정의 : 내가 놓친 거지 무엇에
@RepositoryRestResource(collectionResourceRel = "myEntities", path = "myEntity")
public interface MyEntityRepository extends PagingAndSortingRepository<MyEntity, Long>
, QueryDslPredicateExecutor<MyEntity>
, QuerydslBinderCustomizer<QMyEntity> { }
어떤 생각?
'Resources'의 구조를 보면 JSON 문서의 구조와 다른 점을 알 수 있습니다. 마치 자동차 열쇠로 집을 열려고하는 것과 같습니다. – zeroflagL
그래, 나는 노력하고있는 방식이 효과가 없다는 것을 알았다. 나는 무엇이 효과가 있는지 알아 내려고 노력하고 있습니다. SDR에서이 형식을 생성하는 경우 SDR에서이 형식을 사용하려고 할 때 Spring 클래스가 있어야합니다. 나는 표준 형식으로 내 자신을 굴릴 필요가 없어야한다. – JudgingNotJudging
예제에서 문서의 기초는'PagedResources'입니다. '_embedded'는'EmbeddedWrapper'에 의존합니다. 그러나 문서를 직접 표현하는 클래스는 없습니다. '_embedded' 속성이 동적이기 때문에 문서가 동적으로 빌드됩니다. – zeroflagL