2016-09-12 2 views
0

저는 현재 스프링 우산을 사용하여 API를 만들고 있습니다. 대부분의 컨트롤러는 PagedResources <>을 반환하는 목록 메서드를 제공합니다. 어떤 이유로 든자가 셀에는 모든 예제에있는 {?page,size,sort} 템플릿이 포함되어 있지 않습니다. 대신 기본 URI 만 가져옵니다.Spring HATEOAS PagedResourcesAssembler에 templated selfrel을 추가하는 방법은?

내 ProjectContoller는

@GetMapping 
public PagedResources<ProjectResource> list(Pageable pageable, PagedResourcesAssembler<Project> pagedResourcesAssembler){ 
    Page<Project> projects = service.findAll(pageable); 
    return pagedResourcesAssembler.toResource(projects, assembler); 
} 

모양과 나는 사소한 뭔가를 놓친 것 같아하지만 찾을 수 없습니다

{"_embedded":{ 
    "projectResourceList":[ 
     { 
      "begin":1462053600000, 
      "end":1469829600000, 
      "name":"Cool Big Project", 
      "_links":{"self":{"href":"http://localhost/projects/1"}} 
     } 
    ] 
    }, 
    "_links":{"self":{"href":"http://localhost/projects"}}, 
    "page":{ 
     "size":20, 
     "totalElements":1, 
     "totalPages":1, 
     "number":0 
    } 
} 

반환 : -/

답변

0

HAL이 Spring Data Rest 행동하는 방법을 읽은 후 약간의 버그가있을 수 있습니다.

이것은 tasks 수집 자원에 대한 표준 스프링 데이터 나머지 links 출력은 다음과 같습니다

"_links": { 
    "first": { 
     "href": "http://localhost:8080/tasks?page=0&size=20" 
    }, 
    "self": { 
     "href": "http://localhost:8080/tasks" 
    }, 
    "next": { 
     "href": "http://localhost:8080/tasks?page=1&size=20" 
    }, 
    "last": { 
     "href": "http://localhost:8080/tasks?page=2&size=20" 
    }, 
    "profile": { 
     "href": "http://localhost:8080/profile/tasks" 
    }, 
    "search": { 
     "href": "http://localhost:8080/tasks/search" 
    } 

없음 링크에 표시된 것과 달리 봄 데이터 나머지에 의해 렌더링 박스 HAL 응답,에서의 템플릿되고 있지 docs. 내가 next 링크를 따라하는 경우

self 링크는 올바르지 않습니다 :

"_links": { 
    "first": { 
     "href": "http://localhost:8080/tasks?page=0&size=20" 
    }, 
    "prev": { 
     "href": "http://localhost:8080/tasks?page=0&size=20" 
    }, 
    "self": { 
     "href": "http://localhost:8080/tasks" 
    }, 
    "next": { 
     "href": "http://localhost:8080/tasks?page=2&size=20" 
    }, 
    "last": { 
     "href": "http://localhost:8080/tasks?page=2&size=20" 
    }, 
    "profile": { 
     "href": "http://localhost:8080/profile/tasks" 
    }, 
    "search": { 
     "href": "http://localhost:8080/tasks/search" 
    } 
    } 

나는 컨트롤러 오버라이드 (override)하는 경우 :

@RequestMapping(method = RequestMethod.GET, path = "/tasks") 
    public ResponseEntity<Page<Task>> read(Pageable pageRequest, PersistentEntityResourceAssembler assembler) { 
     Page<Task> pendingTasks = taskService.read(pageRequest); 
     return new ResponseEntity(pageAssembler.toResource(pendingTasks, (ResourceAssembler) assembler), 
            HttpStatus.OK); 
    } 

을 그리고 Pageable를 초기화 컨트롤러와 저장소 사이에 서비스를 추가 인스턴스가 지정되지 않은 경우에도 인스턴스를 기본값으로 설정합니다.

public Page<Task> read(Pageable pageRequest) { 
     Pageable effectivePageRequest = pageRequest; 
     if (effectivePageRequest == null) { 
      effectivePageRequest = new PageRequest(0, 20, DEFAULT_SORT); 
     } 
     if (effectivePageRequest.getSort() == null) { 
      //override Sort 
      effectivePageRequest = new PageRequest(effectivePageRequest.getPageNumber(), 
                effectivePageRequest.getPageSize(), DEFAULT_SORT); 
     } 
     return taskRepository.findByStatus(Task.Status.PENDING, effectivePageRequest); 
    } 

self 링크의 문제를 해결할 수 있습니다. 그러나 템플리트 화 된 URI를 생성 할 방법이 없습니다. 다음은 두 번째 페이지에 대한 링크입니다 :이 가능성을 폐기하는 데 도움이

"_links": { 
    "first": { 
     "href": "http://localhost:8080/tasks?page=0&size=20&sort=created,desc" 
    }, 
    "prev": { 
     "href": "http://localhost:8080/tasks?page=0&size=20&sort=created,desc" 
    }, 
    "self": { 
     "href": "http://localhost:8080/tasks?page=1&size=20&sort=created,desc" 
    }, 
    "next": { 
     "href": "http://localhost:8080/tasks?page=2&size=20&sort=created,desc" 
    }, 
    "last": { 
     "href": "http://localhost:8080/tasks?page=2&size=20&sort=created,desc" 
    } 

희망하지만, 여기에 표시된 기본 동작은 수집 링크를 생성 할 때 봄 HATEOAS/봄 데이터 나머지 몇 가지 문제가있을 수 있음을 시사한다.

내가 org.springframework.boot:spring-boot-starter-data-rest:jar:1.4.0.RELEASE, org.springframework.data:spring-data-rest-webmvc:jar:2.5.2.RELEASE, org.springframework.hateoas:spring-hateoas:jar:0.20.0.RELEASE

+0

같은 결과는 selfrel에서 어떤 템플릿을 사용하고 있습니다. –

+0

자신을 템플릿으로 만들 때 템플릿이 표시됩니까? 예를 들어,'http : // localhost : 8080/projects? size = 2'에 접근하면 –

+0

첫 번째, 다음 그리고 마지막 링크는 잘 동작합니다. 그것들은 각 매개 변수로 올바르게 설정됩니다. selfref의 템플릿 정보가 누락되었습니다 .--( –