栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我可以提供与spring-rest-rest GET并行的端点吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我可以提供与spring-rest-rest GET并行的端点吗?

@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”将命中您的自定义控制器端点。


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/390752.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号