ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – Spring REST重用嵌套的requstmapping

2019-08-28 20:19:49  阅读:225  来源: 互联网

标签:java spring rest spring-mvc api-design


我们有一个REST API,用于一些评论.目前,最有趣的URI是:

GET /products/1/comments             // get all comments of product 1
GET /products/1/comments/5           // get the 5th comment of product 1
GET /products/1/comments/5/user      // get the user of the 5th comment
GET /products/1/comments/latest      // get the latest comment of product 1
GET /products/1/comments/latest/user // get the user of the latest comment

此外,您还可以直接访问评论

GET /comments/987                    // get the comment with id 987
GET /comments/987/user               // get the user of comment with id 987

所以,我们有两个@RestController:

@RestController
@RequestMapping("/products/{productId}")
public class ProductsCommentsResource {

    @GetMapping(value = "/comments")
    public ResponseEntity<?> getComments(@PathVariable Long productId){
       // get all products...
    }

    @GetMapping(value = "/comments/{commentNr}")
    public ResponseEntity<?> getComment(@PathVariable Long productId, @PathVaraible Long commentNr){
       // get comment with number commentNr of product productId
    }

    @GetMapping(value = "/comments/{commentNr}/user")
    public ResponseEntity<?> getCommentUser(@PathVariable Long productId, @PathVaraible Long commentNr){
       // get the user of comment with commentNr of productId
    }

    @GetMapping(value = "/comments/latest")
    public ResponseEntity<?> getLatestComment(@PathVariable Long productId){
       // get latest commentNr and call getComment(productId, commentNr)
    }

    @GetMapping(value = "/comments/latest/user")
    public ResponseEntity<?> getLatestCommentUser(@PathVariable Long productId){
       // get latest commentNr and call getCommentUser(productId, commentNr)
    }
}

@RestController
@RequestMapping("/comments")
public class CommentsResource {
    @GetMapping(value = "/{commentId}")
    public ResponseEntity<?> getComment(@PathVaraible Long commentId){
       // get comment id commentId
    }

    @GetMapping(value = "/{commentId}/user")
    public ResponseEntity<?> getCommentUser(@PathVaraible Long commendId){
       // get the user of comment with id commentId
    }
}      

因此,最新版本只是“获取最后一条注释并使用此commentId调用相应方法”的替换关键字

这只是一个摘录,除了用户之外,评论还有大约30个子资源和子资源(包括POST,DELETE等方法).
因此,我们或多或少都有三次.

所以,显然,我们需要改进这一点,删除重复的代码等.
我们的想法是“封装”评论资源,并通过使用在类中注释的@RequestMapping使其可重用.

我们考虑过像以下一样的机制:

>调用/ products / 1 / comments / latest / user
>呼叫被拦截,产品1的“最新”被解析为commentId
>调用将重定向到“/ comments / {commendId} / user

因此,我们需要有重定向的东西
  – / products / 1 / comments / latest [what ever] to / comments / {commentId} [what ever]
  – / products / 1 / comments / 5 [还有什么]还有/ comments / {commentId} [什么都有]

和/ comments / {commentId}将是唯一的实现.

但是,我们在春季文档中找不到任何合适的内容……

解决方法:

您可以在控制器的@RequestMapping中添加其他URL路径前缀.

@RequestMapping(value = { "/products/{productId}", "/" })

这意味着您可以删除CommentsResource控制器,并且您将能够访问相同的资源:

/products/1/comments/5

并在

/comments/5

例如,这里:

 @GetMapping(value = "/comments/{commentNr}")
    public ResponseEntity<?> getComment(@PathVariable Long productId, @PathVaraible Long commentNr){
       // get comment with number commentNr of product productId
    }

显而易见的问题是productId路径变量.如果您使用的是Java 8,那么使用Optional可以很容易地解决这个问题:

@GetMapping(value = "/comments/{commentNr}")
public ResponseEntity<?> getComment(@PathVariable Optional<Long> productId, @PathVaraible Long commentNr){
    // get comment with number commentNr of product productId
    // Check whether productId exists
}

标签:java,spring,rest,spring-mvc,api-design
来源: https://codeday.me/bug/20190828/1755049.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有