代码之家  ›  专栏  ›  技术社区  ›  Your Friend Ken

如何从@restcontroller返回@repositoryrestresource样式的响应

  •  2
  • Your Friend Ken  · 技术社区  · 8 年前

    // /accounts (RepositoryRestResource JSON structure)
    {
        _embedded: {
            accounts: []
        },
        _links: {
            first: {},
            self: {},
            next: {},
            last: {},
            profile: {},
            search: {}
        },
        page: {
        size: 20,
        totalElements: 35,
        totalPages: 2,
        number: 0
        }
    }
    
    // /my-accounts (RestController JSON structure)
    {
        content: [ ... ], // accounts
        pageable: {},
        totalPages: 1,
        totalElements: 2,
        last: true,
        size: 20,
        number: 0,
        sort: {},
        numberOfElements: 2,
        first: true
    }
    

    REST存储库:

    @RepositoryRestResource(collectionResourceRel = "accounts", path = "accounts", itemResourceRel = "account")
    public interface AccountRepository extends PagingAndSortingRepository<Account, Long> {
    
        @RestResource(path = "owner", rel = "owner")
        Page<Account> findByOwner(@Param("username") String owner,Pageable p);
    }
    

    @RestController
    public class AccountController {
    
        private AccountRepository repo;
    
        @Autowired
        public AccountController(AccountRepository repo) {
            this.repo = repo;
        }
    
        @RequestMapping(
            path = "/my-accounts",
            method = RequestMethod.GET,
            produces = "application/hal+json")
        public ResponseEntity<Page<Account>> getStatus(
            @RequestParam(value = "page", defaultValue = "0", required = false) int page,
            @RequestParam(value = "size", defaultValue = "20", required = false) int size,
            Authentication authentication) {
    
            String username =  authentication.getName();
    
            Page<Account> accounts = repo.findByOwner(username, PageRequest.of(page, size));
    
            return ResponseEntity.ok(accounts);
        }
    }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Danila Kiver    8 年前

    how Spring Data REST does it PagedResourcesAssembler Page

    PagedResourcesAssembler

    public ResponseEntity<PagedResources> getStatus(
        @RequestParam(value = "page", defaultValue = "0", required = false) int page,
        @RequestParam(value = "size", defaultValue = "20", required = false) int size,
        Authentication authentication,
        PagedResourcesAssembler assembler) {
    
        String username =  authentication.getName();
    
        Page<Account> accounts = repo.findByOwner(username, PageRequest.of(page, size));
    
        return ResponseEntity.ok(assembler.toResource(accounts));
    }
    
        2
  •  0
  •   Sebastiaan van den Broek    8 年前

    return ResponseEntity.ok(new org.springframework.hateoas.Resource<>(accounts));
    

    AccountSupport extends ResourceSupport

    add(linkTo(AccountController.class).withSelfRel());
    

    add(linkTo(AccountController.class).slash(idOfYourAccountInstance).withSelfRel())