@PathVariable設置為空的問題(required=false)


參考了: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;
	}

 

原因就是地址是不一樣的,需要配置多個地址映射。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM