java - "PUT"请求未处理

标签 java jquery spring http put

我有一个 jquery 脚本,它使用 PUT 类型将数据发送到我的 spring Controller 。但 Controller 永远不会被击中。 如果我将 PUT 更改为 POST 一切都会按预期工作,但我需要准确使用 PUT。您能检查一下我的代码并告诉我我做错了什么吗?

jQuery

var values = $("form").serialize();
                $.ajax({
                    type: "PUT",
                    url: "/user/" + elogin.val(),
                    async: false,
                    data: values,
                    success: function(resp) {\\doStuff}
                 })

Controller

@Controller
@RequestMapping(value = "/user")
public class RestController {

    @RequestMapping(value = "/{userLogin}", method = RequestMethod.PUT)
    @ResponseBody
    public boolean updateUser(@PathVariable String userLogin,
                              @RequestParam("epassword") String password,
                              ...)
            throws ParseException {
        if (...) {
            return false;
        }
        \\doStuff
        return true;
    }
}

FireBug 错误消息

400 Bad Request
Response Headers
Connection  close
Content-Length  1072
Content-Type    text/html;charset=utf-8
Date    Tue, 03 Sep 2013 10:21:28 GMT
Server  Apache-Coyote/1.1

Request Headers
Accept  */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection  keep-alive
Content-Length  168
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Cookie  JSESSIONID=5A510B1FB82DA6F3DD9E9FA8D67A8295
Host    localhost:8085
Referer http://localhost:8085/welcome
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
X-Requested-With    XMLHttpRequest

错误

HTTP Status 400 - Required String parameter 'epassword' is not present
type Status report
message Required String parameter 'epassword' is not present
description The request sent by the client was syntactically incorrect.

最佳答案

通过添加到web.xml来解决

<filter>
    <filter-name>HttpPutFormContentFilter</filter-name>
    <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>HttpPutFormContentFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

关于java - "PUT"请求未处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18589502/

相关文章:

java - POST 并将嵌套对象持久保存到 Spring MVC

java - HtmlUnit - 获取两个没有 id 的标签之间的文本

java - 当 DatagramSocket 处于接收状态时如何意识到

php - html5 表单没有反应提交

javascript - 如何在jquery/script中传递用户输入数据?我正在使用 codeigniter php 框架

java - 在 Spring 初始化期间终止严重错误的应用程序?

java - Spring boot 2.0.2,使用Aop拦截Cloud Stream注释不再起作用

java - 这是在 Java 中为并发线程计时的正确方法吗

java - str[newLength] = '\0' 是什么意思?

jquery - 转换时缩小 div