SpringMVC之REST风格的URL

    技术2022-07-10  147

    版权声明:本文为 小异常 原创文章,非商用自由转载-保持署名-注明出处,谢谢! 本文网址:https://blog.csdn.net/sun8112133/article/details/107056657

    在 Spring MVC 中支持使用 RESTful 风格的 URL 传参方式,我们来对比一下 传统 URL 传参方式 和 RESTful 风格传参方式:

    传统传参:http://localhost:8080/hello/index?id=10&name=aRESTful 风格传参:http://localhost:8080/hello/index/10/a

    我们在后台这样获取参数:

    @RequestMapping("/rest/{id}/{name}") public String rest(@PathVariable("id") int id, @PathVariable("name") String name) { System.out.println("rest..."); System.out.println("id: " + id); System.out.println("name: " + name); return "index"; }

    注意: @PathVariable 注解是用来获取路径参数的,如果参数名相同时,不需要在括号里指定;若不相同,需要在括号内指定参数。

    博客中若有不恰当的地方,请您一定要告诉我。前路崎岖,望我们可以互相帮助,并肩前行!
    Processed: 0.010, SQL: 9