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

时间:2021-05-19

/** * * 限制路径变量里面的值只能是数字 * 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 /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"

总结

以上所述是小编给大家介绍的SpringMVC请求的路径变量里面写正则表达式的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章