【问题标题】:Error 400--Bad Request错误 400——错误请求
【发布时间】: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


【解决方案1】:

这是因为服务一直在等待路径变量productId

因为您使用的是 Spring 3,我建议您创建 2 个方法。一个带有路径变量,另一个没有它。

 @RequestMapping(value = { "/books/store/product",
                  "/books/store/product/"}, method = { RequestMethod.POST })
    public String saveProduct(@ModelAttribute("productForm") ProductForm productForm,
            HttpServletRequest request, Model model) throws Exception {

..
}

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

..
}

如果您使用的是 Spring 4 和 Java 8,我建议您使用 optional。

@PathVariable Optional<Long> productId

【讨论】:

  • 必须是这个,无论如何:@PathVariable Optional productId
  • 同样的问题 :-(怎么来的?
  • 这是因为 Spring 版本,Optional 适用于 java 8 和 Spring 4 及更高版本。我建议您创建 2 种方法。一个带有路径变量,另一个没有路径变量。我正在编辑答案。
【解决方案2】:

如果您并不总是需要 productId。尝试使用查询参数并将其设为可选。 required=false

这个网址现在看起来像:

  1. http://localhost:8080/books/store/product?productId=232
  2. http://localhost:8080/books/store/product

像这样:

@SuppressWarnings("unchecked")
    @RequestMapping(value = { "/books/store/product",
                   }, method = { RequestMethod.POST })
    public String saveProduct(@ModelAttribute("productForm") ProductForm productForm,
 @RequestParam(value = "productId", required = false) Long productId,
            HttpServletRequest request, Model model) throws Exception {

..
}

希望对你有帮助。

【讨论】:

  • 另外,制作两个单独的方法,一个带有路径变量,另一个没有路径变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 2018-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多