本文主要是介绍解决@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;
}
原因就是地址是不一样的,需要配置多个地址映射。
这篇关于解决@PathVariable(required=false)不起作用的问题_转载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!