[spring boot]spring mvc中的@PathVariable获得请求url中的动态参数

时间:2021-06-17  来源:php常用代码  阅读:

spring mvc中的@PathVariable是用来获得请求url中的动态参数的,十分方便,复习下:

 代码如下

@Controller

publicclassTestController {

   @RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET)

   publicString getLogin(@PathVariable("userId") String userId,

     @PathVariable("roleId") String roleId){

     System.out.println("User Id : "+ userId);

     System.out.println("Role Id : "+ roleId);

     return"hello";

   }

   @RequestMapping(value="/product/{productId}",method = RequestMethod.GET)

   publicString getProduct(@PathVariable("productId") String productId){

      System.out.println("Product Id : "+ productId);

      return"hello";

   }

   @RequestMapping(value="/javabeat/{regexp1:[a-z-]+}",

      method = RequestMethod.GET)

   publicString getRegExp(@PathVariable("regexp1") String regexp1){

      System.out.println("URI Part 1 : "+ regexp1);

      return"hello";

   }

}

[spring boot]spring mvc中的@PathVariable获得请求url中的动态参数

http://m.bbyears.com/jiaocheng/124204.html

推荐访问:spring源码深度解析 springer spring框架
相关阅读 猜你喜欢
本类排行 本类最新