【发布时间】:2018-05-03 17:15:03
【问题描述】:
我正在使用 Spring Boot 开发 REST api。我有一个接受 POST 请求的控制器。
在上述请求中,我想访问参数ride_transection_id 以查找特定的横断对象以及其他一些值。
所以基本上我有 3 种方法可以做到这一点。
1. 我可以使用@PathVariable
@RequestMapping(value = "/end-ride", method = RequestMethod.POST)
public ResponseEntity<?> endRide(@PathVariable("ride_transection_id") long ride_transection_id,@RequestBody
SomeDTORequest someDTORequest ) {
//find transaction using path varibale
}
2.我可以使用@RequestParam
@RequestMapping(value = "/end-ride", method = RequestMethod.POST
public @ResponseBody item getitem(@RequestParam("ride_transection_id")
long ride_transection_id,@RequestBody SomeDTORequest someDTORequest ){
//find transaction using RequestParam varibale
}
-
我可以使用 DTO 对象
SomeDTORequest并接受ride_transection_id以及其他值。@RequestMapping(value = "/end-ride", method = RequestMethod.POST) public ResponseEntity<?> endRide(@RequestBody SomeDTORequest someDTORequest ) { //find transaction using path someDTORequest .getID()}
我有点困惑。只是想问一下访问ride_transection_id 的最安全和正确的方法是什么?
谢谢
【问题讨论】:
-
没有一个正确的答案。这取决于您的要求。你说的这里最安全到底是什么意思?
-
我更喜欢使用 @PathVariable("ride_transection_id") ,因为这是通过 id 接收对象的正确方法(我的观点)。 @RequestParams 更多关于配置/附加参数,以便在获取之前/之后使用数据进行操作。
-
你检查我的答案了吗?
标签: spring rest spring-mvc spring-boot