码迷,mamicode.com
首页 > 其他好文 > 详细

@PathVariable设置为空的问题(required=false)

时间:2019-05-09 11:00:53      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:pre   ref   localhost   http   开始   hang   str   spring   tail   

参考了:http://www.imooc.com/qadetail/268268

 

最近学习springMVC的时候,学到@PathVariable后,发现@PathVariable有个required属性,于是将其设置为false,发现访问请求时报错。

刚开始我的代码是这样的:

	@RequestMapping(value={"/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;
	}

  

后面发现上面的文章,将方法改成如下就可以了:

	/**
	 * 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={"/user/{id}/{name}","/user/{id}","/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)

标签:pre   ref   localhost   http   开始   hang   str   spring   tail   

原文地址:https://www.cnblogs.com/mkl34367803/p/10836959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!