@RepositoryRestController``@RequestMapping在类型级别上玩不好。第一步,通过
produces从RequestMapping中删除参数来确保您确实设法捕获了请求(我在这里使用GetMapping快捷方式)。我还删除了@PreAuthorize注释,因为它目前不相关,并引入了一个参数来捕获
Accept标头值(用于调试):
@RepositoryRestControllerpublic class AccountResource { @GetMapping(value = "/api/accounts") public ResponseEntity<List<Account>> getAll( @RequestParam(value = "page", required = false) Integer offset, @RequestParam(value = "per_page", required = false) Integer limit, @RequestParam(value = "email", required = false) String email, ) throws URISyntaxException { ... }}有了这个,您应该能够随意自定义 GET / api / accounts ,并且仍然可以从Spring Data Rest自动提供的 POST /
PUT / PATCH … / api / accounts中 受益,并断言content-type
如果它按预期工作,则可以:
- 尝试
produces = "application/custom.account+json"
使用GetMapping批注中的方法范围(单个值不需要大括号)来缩小方法范围,并确保您的端点和Spring生成的端点方法均可用 - 恢复您的@PreAuthorize批注
- 摆脱@RequestHeader参数
那给你:
@RepositoryRestController // NO MAPPING AT THE TYPE LEVELpublic class AccountResource { @GetMapping(value = "/api/accounts", // Mapping AT THE METHOD LEVEL produces = "application/custom.account+json") // the content-type this method answers to @PreAuthorize("#oauth2.hasScope('read') and hasRole('ADMIN')") // ROLE is 'ADMIN' not 'ROLE_ADMIN' public ResponseEntity<List<Account>> getAll( @RequestHeader("Content-Type") String contentType, @RequestParam(value = "page", required = false) Integer offset, @RequestParam(value = "per_page", required = false) Integer limit, @RequestParam(value = "email", required = false) String email, ) throws URISyntaxException { ... }}现在:
- curl host:port / api / accounts将命中Spring控制器端点
- curl host:port / api / accounts -H“接受:application / custom.account + json”将命中您的自定义控制器端点。



