关于用@pathvariable实现响应多个渠道的请求

来源:互联网 发布:单片机调试过程 编辑:程序博客网 时间:2024/05/05 15:58

spring mvc中的@PathVariable是用来获得请求url中的动态参数的

  1. @Controller  
  2. public class TestController {  
  3.      @RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET)  
  4.      public String getLogin(@PathVariable("userId") String userId,  
  5.          @PathVariable("roleId") String roleId){  
  6.          System.out.println("User Id : " + userId);  
  7.          System.out.println("Role Id : " + roleId);  
  8.          return "hello";  
  9.      }  
  10.      @RequestMapping(value="/product/{productId}",method = RequestMethod.GET)  
  11.      public String getProduct(@PathVariable("productId") String productId){  
  12.            System.out.println("Product Id : " + productId);  
  13.            return "hello";  
  14.      }  
  15.      @RequestMapping(value="/javabeat/{regexp1:[a-z-]+}",  
  16.            method = RequestMethod.GET)  
  17.      public String getRegExp(@PathVariable("regexp1") String regexp1){  
  18.            System.out.println("URI Part 1 : " + regexp1);  
  19.            return "hello";  
  20.      }  
  21. }
最好不要把动态参数放在类名上的requestMapping()中,这样就不够灵活,首先保证所有的请求都可以进来到类映射

0 0
原创粉丝点击