【发布时间】:2017-08-04 20:27:30
【问题描述】:
我有一个基于 Spring Web 模型-视图-控制器 (MVC) 框架的项目。 Spring Web 模型-视图-控制器(MVC)框架的版本是 3.2.8
我有这个控制器
@SuppressWarnings("unchecked")
@RequestMapping(value = { "/books/store/product",
"/books/store/product/",
"/books/store/product/{productId}",
"/books/store/product/{productId}/" }, method = { RequestMethod.POST })
public String saveProduct(@ModelAttribute("productForm") ProductForm productForm,
@PathVariable Long productId,
HttpServletRequest request, Model model) throws Exception {
..
}
这个网址一切正常:/books/store/product/232
但是对于这个/books/store/product/
我收到了这个错误:
错误 400--错误请求
From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1:
10.4.1 400 Bad Request
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.
我试过把这个@PathVariable(required = false) 放上去,但是我得到了一个编译错误:The attribute required is undefined for the annotation type PathVariable
【问题讨论】:
-
你应该添加@PathVariable(required=false)
-
最好把它分成两个方法,一个有PathVariable,一个没有,也不需要加“/”映射,没用
标签: java spring spring-mvc