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

时间:2012-06-21 20:04:00

标签: java javascript spring hibernate methods

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

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

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

jsp页面的一部分

<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>

控制器的一部分:

@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。我仍然得到HTTP Status 405 - Request method 'POST' not supported错误。

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

// D

6 个答案:

答案 0 :(得分:21)

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

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

<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>

答案 1 :(得分:15)

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

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

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

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

HTTP状态405 - 请求方法&#39; POST&#39;不支持

过了一段时间我发现该方法实际上已被调用,但由于没有@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();
}

答案 2 :(得分:5)

您可能需要更改行

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

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

答案 3 :(得分:3)

问题是您的控制器需要参数 hasId = false hasId = true ,但您没有传递该参数。您的隐藏字段的ID为 hasId ,但是作为 hasCustomerName 传递,因此没有映射匹配。

将隐藏字段的路径更改为 hasId 或将映射参数更改为 hasCustomerName = true hasCustomerName = false

答案 4 :(得分:3)

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

在“保存”按钮触发的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ö感谢您发现我的错误,我没有传递正确的参数!

答案 5 :(得分:0)

由于其他原因,我遇到了类似的问题(csrf令牌中未添加网址格式test-response) 我通过在config/local.properties的以下属性中允许我的URL模式来解决了该问题:

csrf.allowed.url.patterns = /[^/]+(/[^?])+(sop-response)$,/[^/]+(/[^?])+(merchant_callback)$,/[^/]+(/[^?])+(hop-response)$

修改为

csrf.allowed.url.patterns = /[^/]+(/[^?])+(sop-response)$,/[^/]+(/[^?])+(merchant_callback)$,/[^/]+(/[^?])+(hop-response)$,/[^/]+(/[^?])+(test-response)$