SpringMVC请求的路径变量里面写正则表达式的方法
时间:2020-11-11 11:52:41|栏目:JAVA代码|点击: 次
/** * * 限制路径变量里面的值只能是数字 * 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 PageThis 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"
总结
栏 目:JAVA代码
下一篇:Java notify和notifyAll的区别和相同
本文标题:SpringMVC请求的路径变量里面写正则表达式的方法
本文地址:http://www.codeinn.net/misctech/21555.html
阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机