SpringMvc中的@RequestMapping的用法简介说明
下文笔者讲述RequestMapping的几种用法简介说明,如下所示:
其中用(@PathVariable接收rest风格的参数 @RequestMapping(value="/users/{userId}") public String findUserInfo(@PathVariable String userId){ System.out.println("Find User with ID: " + userId); return "info"; }
方法上使用RequestMapping
@RequestMapping(value="/users") public String testFun(){ System.out.println("方法被运行!"); return "info"; }
参数绑定
如:/users?userId=888@RequestMapping(value="/users") public String findUserInfo( @RequestParam("userId") String userId){ System.out.println("userId: " + userId); return "info"; }
REST风格的参数
如:/users/88其中用(@PathVariable接收rest风格的参数 @RequestMapping(value="/users/{userId}") public String findUserInfo(@PathVariable String userId){ System.out.println("Find User with ID: " + userId); return "info"; }
url中同时绑定多个id
@RequestMapping(value="/users/{userId}/employees/{employeeId}") public String findEmployee( @PathVariable String userId, @PathVariable String employeeId){ System.out.println("employeeId: " + employeeId + " user: " + userId); return "info"; }
支持正则表达式
@RequestMapping(value="/{textualPart:[a-z-]+}.{numericPart:[\\d]+}") public String regularExpression( @PathVariable String textualPart, @PathVariable String numericPart){ System.out.println("Textual part: " + textualPart + ", numeric part: " + numericPart); return "info"; }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。