spring-mvc - 比较:@RequestMapping 与 @PathVariable 的用法
问题描述
我想知道以上哪一项更好/正确/最常用或什么。第一个,使用 @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
两者都有效。就是想知道两者的区别。
提前致谢!
解决方案
JPA是 Java Persistence API,与 ; 无关@RequestMapping
;
您正在询问@RequestMappingpath
之间的区别:value
就是想知道两者的区别。
path
和value
元素是彼此的别名,我只能从Spring 文档中看到细微的差异:
使用时path
:
- 还支持 Ant 风格的路径模式(例如
"/profile/**"
); "/${profile_path}"
可以使用占位符(例如)。
推荐阅读
- java - 有没有办法在 JBehave 故事中定义两条路径
- python - 我如何比较这两个字典(其中一个是嵌套的)?[Python]
- android-layout - BoxConstraints 在拉伸容器时强制使用无限高
- php - 有没有办法这行代码可以工作 header("Location: ../login.php");
- c++ - 我正在尝试将 DMA 数组及其大小作为参数传递,但它给出了错误
- node.js - 聚合后我无法读取数据...在nodejs中
- jdbc - [Snowflake-jdbc]从connection.getMetadata().getColumns(...)的resetset对象获取信息时挂起
- vue.js - Vue Js:重用表单组件进行添加/编辑操作
- google-app-engine - Google App Engine 防火墙和内部访问和错误 403
- javascript - 为什么我在使用自定义 HTML 标签时会在 Vue 中出现错误?