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

网友投稿 396 2023-07-20

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

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;

/** * 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;}

Whitelabel Error Page

This application has no explicit mapping for /ehttp://rror, 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"

总结

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:基于String实现同步锁的方法步骤
下一篇:SpringMVC 单文件,多文件上传实现详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~