@RequestParam和@PathVariable都能够完成类似的功能——因为本质上,他们都是用户的输入,只不过输入的部分不同,一个在URL路径部分,另一个在参数部分。我们要访问一篇博客文章,这两种URL设计都是可以的:
通过@PathVariable,例如/blogs/1
通过@RequestParam,例如blogs?blogId=1
那么究竟应该如何选择呢?我们建议:
当URL指向的是某一具体业务资源(或者资源列表),例如博客、用户时,使用@PathVariable
当URL需要对资源或者资源列表进行过滤、筛选时,用@RequestParam
例如我们会这样设计URL:
/blogs/{blogId}
/blogs?state=publish而不是/blogs/state/publish来表示处于发布状态的博客文章
@RequestParam和@PathVariable
原创mb5c80f4c73b73a 博主文章分类:JAVA相关 ©著作权
©著作权归作者所有:来自51CTO博客作者mb5c80f4c73b73a的原创作品,请联系作者获取转载授权,否则将追究法律责任

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Softmax和Sigmoid应用之——CLIP和SiGLIP
Softmax和Sigmoid函数在多模态图像嵌入模型中的运用。
损失函数 多模态 CLIP SigLIP AIGC的底层技术 -
@PathVariable和@RequestParam
@PathVariable和@RequestParam的区别请求路径http://localhost:8080/Springmvc/us
java json xhtml 示例代码 xml -
@RequestParam和@PathVariable的作用
全面解析RequestParam注解、PathVariable注解使用和区别。
java 注解 RequestParam PathVariable Sring注解使用 -
SpringMVC(1)----@RequestParam和@PathVariable用法小结
1、
url spring mvc 前端 Java 架构师 -
springmvc的@RequestMapping、@PathVariable、@RequestParam
@RequestMapping("/somepath/{userName}") public String register(@PathVariable(value = "userName") String userName) { ModelAndView mav = new ModelAndView(); return "user/createSuccess"; } 在springmvc注解的@
springmvc @RequestMapping @PathVariable @RequestParam