简体   繁体   English

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

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

Im getting this error: HTTP Status 405 - Request method 'POST' not supported我收到此错误: 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.例如,当我在customerName框中选择一个名称时,.jsp 页面中的onChange函数应该运行,然后提交的页面再次加载customerCountry框中的相应值。

however I'm getting this HTTP Status 405 error.但是我收到了这个 HTTP Status 405 错误。 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 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>

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 .编辑:hasId更改为hasCustomerName I still get the HTTP Status 405 - Request method 'POST' not supported error though.我仍然收到HTTP Status 405 - Request method 'POST' not supported错误。

EDIT2: Commented out the line in the setFalse function that was causing the error EDIT2:注释掉导致错误的setFalse函数中的行

// D // D

I am not sure if this helps but I had the same problem.我不确定这是否有帮助,但我遇到了同样的问题。

You are using springSecurityFilterChain with CSRF protection.您正在使用具有 CSRF 保护的 springSecurityFilterChain。 That means you have to send a token when you send a form via POST request.这意味着当您通过 POST 请求发送表单时,您必须发送一个令牌。 Try to add the next input to your form:尝试将下一个输入添加到您的表单中:

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

Check if you are returning a @ResponseBody or a @ResponseStatus检查您返回的是 @ResponseBody 还是 @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:使用 POST 请求调用时,我总是收到以下错误:

HTTP Status 405 - Request method 'POST' not supported HTTP 状态 405 - 不支持请求方法“POST”

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.过了一会儿我发现该方法实际上被调用了,但是因为没有@ResponseBody 和@ResponseStatus Spring MVC 引发了错误。

To fix this simply add a @ResponseBody要解决此问题,只需添加一个 @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.或您的方法的@ResponseStatus。

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

You might need to change the line您可能需要更改行

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

to

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

The problem is that your controller expect a parameter hasId=false or hasId=true , but you are not passing that.问题是您的控制器需要一个参数hasId=falsehasId=true ,但您没有传递它。 Your hidden field has the id hasId but is passed as hasCustomerName , so no mapping matches.您的隐藏字段具有 id hasId但作为hasCustomerName传递,因此没有映射匹配。

Either change the path of the hidden field to hasId or the mapping parameter to expect hasCustomerName=true or hasCustomerName=false .将隐藏字段的路径更改为hasId或将映射参数更改为期望hasCustomerName=truehasCustomerName=false

I found the problem that was causing the HTTP error.我发现了导致 HTTP 错误的问题。

In the setFalse() function that is triggered by the Save button my code was trying to submit the form that contained the button.在由“保存”按钮触发的setFalse()函数中,我的代码试图提交包含该按钮的表单。

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

when I remove the document.submitForm.submit();当我删除document.submitForm.submit(); it works:有用:

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

@Roger Lindsjö Thank you for spotting my error where I wasn't passing on the right parameter! @Roger Lindsjö 感谢您发现我没有传递正确参数的错误!

I was getting similar problem for other reason (url pattern test-response not added in csrf token) I resolved it by allowing my URL pattern in following property in config/local.properties :由于其他原因,我遇到了类似的问题(未在 csrf 令牌中添加 url 模式test-response )我通过在config/local.properties的以下属性中允许我的 URL 模式来解决它:

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

modified to修改为

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

In my case the url was ending with /在我的例子中,url 以 / 结尾

paymentUrl(old)= /get-details/ paymentUrl(old)= /get-details/

i just removed the trailing /我刚刚删除了尾随/

paymentUrl(new)= /get-details paymentUrl(new)= /get-详细信息

and it worked它奏效了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTTP状态405-Spring MVC不支持请求方法&#39;POST&#39; - HTTP Status 405 - Request method 'POST' not supported Spring MVC Spring MVC - HTTP 状态 405 - 不支持请求方法“POST” - Spring MVC - HTTP Status 405 - Request method 'POST' not supported Spring MVC HTTP状态405-不支持请求方法“ POST” - Spring MVC HTTP Status 405 - Request method 'POST' not supported HTTP状态405 - 使用Spring Security的Spring MVC中不支持请求方法'POST' - HTTP Status 405 - Request method 'POST' not supported in Spring MVC with Spring Security 具有Spring Security的Spring-mvc获得HTTP状态405-请求方法&#39;POST&#39;不支持 - Spring-mvc with spring security getting HTTP Status 405 - Request method 'POST' not supported Spring MVC 上传文件 - HTTP 状态 405 - 不支持请求方法“POST” - Spring MVC upload file - HTTP Status 405 - Request method 'POST' not supported Spring MVC 请求方法“POST”不支持-&gt; HTTP 405 - Spring MVC Request method 'POST' not supported -> HTTP 405 HTTP状态405-在Spring MVC中执行返回“重定向:*。*”时,不支持请求方法“ GET” - HTTP Status 405 - Request method 'GET' not supported when doing return “redirect:*.*” in spring mvc HTTP状态405-请求方法&#39;POST&#39;不支持Spring Security Java Config - HTTP Status 405 - Request method 'POST' not supported Spring Security Java Config Spring MVC:HTTP 405-发出POST请求时不支持请求方法“ GET” - Spring MVC: HTTP 405 - Request method 'GET' not supported when making POST request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM