解决@PathVariable(required=false)不起作用的问题_转载
·
声明
本文转载自https://www.cnblogs.com/mkl34367803/p/10836959.html
正文
最近学习springMVC,学到@PathVariable时,发现@PathVariable有个required属性,尝试将其设置为false,但请求/helloWorld/user时报404。
刚开始我的代码是这样的:
@RequestMapping(value={"/helloWorld/user/{id}/{name}"})
public User getUser(@PathVariable(value="id",required=false) Integer id,@PathVariable(value="name",required=false) String name ){
System.out.println("--------------:"+id+","+name);
User user=new User(id,name);
return user;
}
改成下面的样子,请求/helloWorld/user的时候,就不会报404了。
/**
* http://localhost:8080/helloWorld/user/1/zhangsan
* http://localhost:8080/helloWorld/user/1
* http://localhost:8080/helloWorld/user
* @param id
* @param name
* @return
*/
@RequestMapping(value={"/helloWorld//user/{id}/{name}","/helloWorld/user/{id}","/helloWorld//user"})
public User getUser(@PathVariable(value="id",required=false) Integer id,@PathVariable(value="name",required=false) String name ){
System.out.println("--------------:"+id+","+name);
User user=new User(id,name);
return user;
}
原因就是地址是不一样的,需要配置多个地址映射。
更多推荐



所有评论(0)