如何将资源位置添加到响应中?

时间:2018-08-21 12:56:15

标签: spring-boot spring-restcontroller spring-rest

我找不到在控制器端点的响应中添加资源位置的方法:

@GetMapping
@ResponseBody
public ResponseEntity<PagedResources<UserResource>> all(Pageable pageable,
        PagedResourcesAssembler<User> pagedResourcesAssembler, UriComponentsBuilder builder) {
    Page<User> foundUsers = userService.all(pageable);
    URI location = builder.path(RESTConstants.SLASH + DomainConstants.USERS).queryParam("page", pageable.getPageNumber())
    .queryParam("size", pageable.getPageSize()).buildAndExpand().toUri();
    Link selfLink = linkTo(methodOn(UserController.class).all(pageable, pagedResourcesAssembler, builder))
            .withRel("all");
    PagedResources<UserResource> userPagedResources = pagedResourcesAssembler.toResource(foundUsers,
            userResourceAssembler, selfLink);
    return ResponseEntity.ok(userPagedResources);
}

我应该在200 OK响应中放置这样的位置吗?

在另一个端点上,我还希望具有描述分页的位置:

@GetMapping(params = "searchTerm")
@ResponseBody
public ResponseEntity<PagedResources<UserResource>> search(@RequestParam(value = "searchTerm") String searchTerm,
        Pageable pageable, PagedResourcesAssembler<User> pagedResourcesAssembler, UriComponentsBuilder builder) {
    Page<User> foundUsers = userService.search(searchTerm, pageable);
    URI location = builder.path(RESTConstants.SLASH + DomainConstants.USERS)
    .queryParam("searchTerm", searchTerm).queryParam("page", pageable.getPageNumber())
    .queryParam("size", pageable.getPageSize()).buildAndExpand().toUri();
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setLocation(location);
    Link selfLink = linkTo(
            methodOn(UserController.class).search(searchTerm, pageable, pagedResourcesAssembler, builder))
                    .withRel("search");
    PagedResources<UserResource> userPagedResources = pagedResourcesAssembler.toResource(foundUsers,
            userResourceAssembler, selfLink);
    return ResponseEntity.ok(userPagedResources);
}

最后,未授权状态如何?有没有比这更好的方法了?

@ExceptionHandler(CannotEncodePasswordException.class)
@ResponseBody
public ResponseEntity<ErrorInfo> cannotEncodePasswordException(HttpServletRequest request, CannotEncodePasswordException e) {
    String url = request.getRequestURL().toString();
    String errorMessage = localizeErrorMessage(e.getLocalizedMessage());
    ErrorInfo errorInfo = new ErrorInfo(url, HttpStatus.UNAUTHORIZED, errorMessage);
    logger.error(errorMessage);
    logger.debug(getStackTrace(e));
    return new ResponseEntity<ErrorInfo>(errorInfo, HttpStatus.UNAUTHORIZED);
}

更新:这是我添加位置的方式

@GetMapping
@ResponseBody
public ResponseEntity<PagedResources<UserResource>> all(Pageable pageable,
        PagedResourcesAssembler<User> pagedResourcesAssembler, UriComponentsBuilder builder) {
    Page<User> foundUsers = userService.all(pageable);
    Link selfLink = linkTo(methodOn(UserController.class).all(pageable, pagedResourcesAssembler, builder))
    .withRel("all");
    PagedResources<UserResource> userPagedResources = pagedResourcesAssembler.toResource(foundUsers,
    userResourceAssembler, selfLink);
    URI location = builder.path(RESTConstants.SLASH + DomainConstants.USERS)
        .queryParam("page", pageable.getPageNumber()).queryParam("size", pageable.getPageSize())
        .buildAndExpand().toUri();
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setLocation(location);
    return ResponseEntity.status(HttpStatus.OK).headers(responseHeaders).body(userPagedResources);
}

0 个答案:

没有答案