2014-09-03 2 views
3

내 REST 컨트롤러에서 spring-data-common의 PagedResourcesAssembler를 사용하고 있으며 응답에서 다음/이전 링크를 생성하는 것을 보아서 기뻤습니다. 그러나 추가 쿼리 매개 변수 (페이지, 크기, 정렬 외)가있는 경우 생성 된 링크에 포함되지 않습니다. 어떻게 든 링크에 매개 변수를 포함하도록 어셈블러를 구성 할 수 있습니까?PagedResourcesAssembler를 사용하여 추가 쿼리 매개 변수가있는 링크 생성

많은 감사, 다니엘

답변

4

당신은 스스로베이스 링크를 구축하고 PagedResourcesAssembler의에서 "toResource"방법을 전달해야합니다.

http://[your host]/[your app context]/offer/search/findById?offerId=[some offer id]{&page,size,sort} 
+0

나는 당신이이 질문에 사용 된 링크의 동일한 유형을 사용하려고하지만 나를 위해 작동하지 않았다

@Controller @RequestMapping(value = "/offer") public class OfferController { private final OfferService offerService; private final OfferAssembler offerAssembler; @Autowired public OfferController(final OfferService offerService, OfferAssembler offerAssembler) { this.offerService= checkNotNull(offerService); this.offerAssembler= checkNotNull(offerAssembler); } @RequestMapping(value = "/search/findById", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE }) public ResponseEntity<PagedResources<OfferResource>> findOfferById( @RequestParam(value = "offerId") long offerId, Pageable pageable, PagedResourcesAssembler<OfferDetails> pagedResourcesAssembler) { Page<OfferDetails> page = service.findById(offerId, pageable); Link link = linkTo(methodOn(OfferController.class).findOfferById(offerId, pageable, pagedResourcesAssembler)).withSelfRel(); PagedResources<OfferResource> resource = pagedResourcesAssembler.toResource(page, assembler, link); return new ResponseEntity<>(resource, HttpStatus.OK); } } 

결과적으로 당신은 얻을 것이다. Link Link = new Link (인벤토리 클래스, "이름", "페이지", "크기", "정렬") toString() + "/ search/categoryName {? name, 페이지, 크기, 정렬} "). withSelfRel()'. 나는 그걸 찔러보고 너와 같이 일할 수 있는지 알아 보겠다.하지만 지금은 그 일이 잘되어있어. –