0

我想知道以上哪一项更好/正确/最常用或什么。第一个,使用 @RequestMapping 中的值或其他使用路径。

    @RequestMapping(value = { "/isbn/{isbnCode}" }, method = RequestMethod.GET)
    public ResponseEntity<?> findByIsbnCode(@PathVariable String isbnCode) {
        Book obj = service.findByIsbnCode(isbnCode);

        return ResponseEntity.ok().body(obj);
    }
// Request: http://localhost:8080/books/title?title=book_title

    @RequestMapping(method = RequestMethod.GET, path = { "/title" })
    public ResponseEntity<?> findByTitle(@RequestParam(value = "title") String title) {
        Book obj = service.findByTitle(title);

        return ResponseEntity.ok().body(obj);
    }
// Request: http://localhost:8080/books/isbn/978-84-663-4612-2

两者都有效。就是想知道两者的区别。

提前致谢!

4

2 回答 2

1

JPA是 Java Persistence API,与 ; 无关@RequestMapping

您正在询问@RequestMappingpath之间的区别:value

就是想知道两者的区别。

pathvalue元素是彼此的别名,我只能从Spring 文档中看到细微的差异:

使用时path

  • 还支持 Ant 风格的路径模式(例如"/profile/**");
  • "/${profile_path}"可以使用占位符(例如)。
于 2020-09-23T14:22:37.360 回答
1

“URI 参数(由@PathVariable 表示的路径参数/变量)基本上用于标识一个或多个特定资源,而查询参数(或由@RequestParam 表示的请求参数)用于对这些资源进行排序/过滤。” 第一个示例使用路径变量,在这里您从 url 本身提取值,而在第二个示例中,您正在获取请求/查询参数。参考资料: https ://dzone.com/articles/understanding-the-uri-param-and-query-param-with-r https://blog.restcase.com/7-rules-for-rest-api-uri -design/#:~:text=URIs%20should%20follow%20a%20predictable,APIs%20are%20written%20for%20consumers

于 2020-09-22T07:26:30.053 回答