SpringMvc请求的路径变量里面写正则表达式的方法

/**
 *
 * 限制路径变量里面的值只能是数字
 * http://localhost:8080/test/getUser/1
 * http://localhost:8080/test/getUser/a
 */
@RequestMapping("/getUser/{id:\\d+}")
public User getUser(@PathVariable(name = "id") String userId) {
  User user = new User();
  user.setUsername(userId);
  user.setPassword(userId);
  return user;
}
public class User {
  private String username;

6DDE5B43-5C5E-FAFE-BFC9-07F05384BC55.png

0FCFB93E-231A-106B-D5BA-A69070A81A2E.png

/** * http://localhost:8080/test/getUser02/1 
* http://localhost:8080/test/getUser02/a */@RequestMapping("/getUser02/{id}")
public User getUser02(@PathVariable(name = "id")
 Integer userId) 
{  
 User user = new User(); 
 user.setUsername(String.valueOf(userId));  user.setPassword(String.valueOf(userId));  
return user;}

58E68851-F911-0873-D84D-89E40ADA3C79.png

9C522928-5FFD-48AE-0573-0C282B6191F6.png

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Sep 22 10:57:52 CST 2019

There was an unexpected error (type=Bad Request, status=400).

Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "a"

收藏 (0)
评论列表
正在载入评论列表...
我是有底线的