HTTP状态405 – 不支持请求方法“POST”(Spring MVC)

我收到此错误: HTTP Status 405 - Request method 'POST' not supported

我要做的是创建一个带有下拉框的表单,该表单将根据在另一个下拉框中选择的其他值进行填充。 例如,当我在customerName框中选择一个名称时,应运行.jsp页面中的onChange函数,然后提交页面,然后再次使用customerCountry框中的相应值加载。

但是我收到此HTTP状态405错误。 我已经在互联网上搜索了一个解决方案,但却找不到任何有用的东西。 这是我的代码的相关部分:

jsp页面的一部分

    Insert title here  .error { color: red; }   function repopulate(){ document.deliveryForm.submit(); } function setFalse(){ document.getElementById("hasId").value ="false"; document.deliveryForm.submit(); // document.submitForm.submit(); (This was causing the error) }    

Create New Delivery

控制器的一部分:

 @RequestMapping(value = "/add", method = RequestMethod.GET) public String getDelivery(ModelMap model) { DeliveryDto deliveryDto = new DeliveryDto(); model.addAttribute("deliveryDtoAttribute", deliveryDto); model.addAttribute("customerNameList", customerService.listAllCustomerNames()); model.addAttribute("customerCountryList", customerService .listAllCustomerCountries(deliveryDto.getCustomerName())); return "new-delivery"; } // I want to enter this method if hasId=true which means that a value in the CustomerName // drop down list was selected. This should set the CountryList to the corresponding values // from the database. I want this post method to be triggered by the onChange in the jsp page @RequestMapping(value = "/add", method = RequestMethod.POST, params="hasCustomerName=true") public String postDelivery( @ModelAttribute("deliveryDtoAttribute") DeliveryDto deliveryDto, BindingResult result, ModelMap model) { model.addAttribute("deliveryDtoAttribute", deliveryDto); model.addAttribute("customerNameList", customerService.listAllCustomerNames()); model.addAttribute("customerCountryList", customerService .listAllCustomerCountries(deliveryDto.getCustomerName())); return "new-delivery"; } // This next post method should only be entered if the save button is hit in the jsp page @RequestMapping(value = "/add", method = RequestMethod.POST, params="hasCustomerName=false") public String postDelivery2( @ModelAttribute("deliveryDtoAttribute") @Valid DeliveryDto deliveryDto, BindingResult result, ModelMap model) { if (result.hasErrors()) { model.addAttribute("deliveryDtoAttribute", deliveryDto); model.addAttribute("customerNameList", customerService.listAllCustomerNames()); model.addAttribute("customerCountryList", customerService .listAllCustomerCountries(deliveryDto.getCustomerName())); return "new-delivery"; } else { Delivery delivery = new Delivery(); //Setters to set delivery values return "redirect:/mis/home"; } } 

我怎么会得到这个错误? 任何帮助将非常感激! 谢谢

编辑: hasId hasCustomerName更改为hasCustomerName 。 我仍然得到HTTP Status 405 - Request method 'POST' not supported错误。

EDIT2: setFalse导致错误的setFalse函数中的行

// D.

我不确定这是否有帮助,但我遇到了同样的问题。

您正在使用带有CSRF保护的springSecurityFilterChain。 这意味着您必须在通过POST请求发送表单时发送令牌。 尝试将下一个输入添加到表单:

  

检查您是否正在返回@ResponseBody或@ResponseStatus

我有类似的问题。 我的控制器看起来像这样:

 @RequestMapping(value="/user", method = RequestMethod.POST) public String updateUser(@RequestBody User user){ return userService.updateUser(user).getId(); } 

使用POST请求调用时,我总是遇到以下错误:

HTTP状态405 – 不支持请求方法“POST”

过了一会儿,我发现该方法实际上已被调用,但由于没有@ResponseBody且没有@ResponseStatus,因此Spring MVC会引发错误。

要解决此问题,只需添加@ResponseBody即可

 @RequestMapping(value="/user", method = RequestMethod.POST) public @ResponseBody String updateUser(@RequestBody User user){ return userService.updateUser(user).getId(); } 

或者@ResponseStatus到您的方法。

 @RequestMapping(value="/user", method = RequestMethod.POST) @ResponseStatus(value=HttpStatus.OK) public String updateUser(@RequestBody User user){ return userService.updateUser(user).getId(); } 

您可能需要更改该行

 @RequestMapping(value = "/add", method = RequestMethod.GET) 

 @RequestMapping(value = "/add", method = {RequestMethod.GET,RequestMethod.POST}) 

问题是您的控制器期望参数hasId = falsehasId = true ,但您没有传递它。 您的隐藏字段具有id hasId但是作为hasCustomerName传递,因此没有映射匹配。

将隐藏字段的路径更改为hasId,或将映射参数更改为expectCustomerName = truehasCustomerName = false

我发现导致HTTP错误的问题。

在由Save按钮触发的setFalse()函数中,我的代码试图提交包含按钮的表单。

  function setFalse(){ document.getElementById("hasId").value ="false"; document.deliveryForm.submit(); document.submitForm.submit(); 

当我删除document.submitForm.submit(); 有用:

  function setFalse(){ document.getElementById("hasId").value ="false"; document.deliveryForm.submit() 

@RogerLindsjö感谢您发现我的错误,我没有传递正确的参数!

Customer Name
Customer Country