Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Status 405 - Request method 'POST' not supported (Spring MVC)

Tags:

Im getting this error: HTTP Status 405 - Request method 'POST' not supported

What I am trying to do is make a form with a drop down box that get populated based on the other value selected in another drop down box. For example when I select a name in the customerName box the onChange function in the .jsp page should be run and the page submitted then loaded again with the corresponding values in the customerCountry box.

however I'm getting this HTTP Status 405 error. I have searched the internet for a solution but haven't been able to find anything that helped. Here is the relevant parts of my code:

part of jsp page

<html>     <head>         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">         <title>Insert title here</title>             <style>             .error { color: red; }             </style>          <script>             function repopulate(){                   document.deliveryForm.submit();             }              function setFalse(){                 document.getElementById("hasId").value ="false";                 document.deliveryForm.submit();                 // document.submitForm.submit(); (This was causing the error)              }         </script>      </head>     <body>          <h1>Create New Delivery</h1>          <c:url var="saveUrl" value="/test/delivery/add" />         <form:form modelAttribute="deliveryDtoAttribute" method="POST" action="${saveUrl}" name="deliveryForm">             <table>                   <tr>                     <td><form:hidden id="hasId" path="hasCustomerName" value="true"/></td>                 </tr>                  <tr>                     <td>Customer Name</td>                     <td><form:select path="customerName" onChange="repopulate()">                         <form:option value="" label="--- Select ---" />                         <form:options items="${customerNameList}" />                         </form:select>                     </td>                     <td><form:errors path="customerName" cssClass="error" /></td>                 </tr>                  <tr>                     <td>Customer Country</td>                     <td><form:select path="customerCountry">                         <form:option value="" label="--- Select ---" />                         <form:options items="${customerCountryList}" />                         </form:select>                     </td>                     <td><form:errors path="customerCountry" cssClass="error" /></td>                 </tr>          </form:form>          <form:form name="submitForm">         <input type="button" value="Save" onClick="setFalse()"/>         </form:form>      </body> </html> 

part of controller:

@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";         }      } 

How come I get this error? Any help would be much appreciated! Thanks

EDIT: Changed hasId to hasCustomerName. I still get the HTTP Status 405 - Request method 'POST' not supported error though.

EDIT2: Commented out the line in the setFalse function that was causing the error

// D

like image 357
dlinx90 Avatar asked Jun 21 '12 20:06

dlinx90


People also ask

How do I fix 405 Method not allowed in spring boot?

405 Not Support – Reason, Solution As you can expect, we can solve this by defining an explicit mapping for PUT, in the existing method mapping: @RequestMapping( value = "/employees", produces = "application/json", method = {RequestMethod.


2 Answers

I am not sure if this helps but I had the same problem.

You are using springSecurityFilterChain with CSRF protection. That means you have to send a token when you send a form via POST request. Try to add the next input to your form:

<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> 
like image 116
Ashish Avatar answered Oct 08 '22 13:10

Ashish


Check if you are returning a @ResponseBody or a @ResponseStatus

I had a similar problem. My Controller looked like that:

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

When calling with a POST request I always got the following error:

HTTP Status 405 - Request method 'POST' not supported

After a while I figured out that the method was actually called, but because there is no @ResponseBody and no @ResponseStatus Spring MVC raises the error.

To fix this simply add a @ResponseBody

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

or a @ResponseStatus to your method.

@RequestMapping(value="/user", method = RequestMethod.POST) @ResponseStatus(value=HttpStatus.OK) public String updateUser(@RequestBody User user){     return userService.updateUser(user).getId(); } 
like image 23
RenRen Avatar answered Oct 08 '22 12:10

RenRen