java - 已经在web.xml中配置过滤器,为何无法提交put请求?

 li永不言败ly_608 发布于 2022-10-25 11:07

web.xml中配置过滤器

    
        HiddenHttpMethodFilter
        org.springframework.web.filter.HiddenHttpMethodFilter
    
    
        HiddenHttpMethodFilter
        /*
    

jsp中使用put提交ajax请求

        //提交到后台的RESTful
        $.ajax({
           type: "PUT",
           url: "/rest/item",
           data: $("#itemeEditForm").serialize(),
           statusCode:{
               204:function(){
                   $.messager.alert('提示','修改商品成功!','info',function(){
                        $("#itemEditWindow").window('close');
                        $("#itemList").datagrid("reload");
                    });
               },
               500:function(){
                   $.messager.alert('提示','修改商品失败!');
               }
           }
    
        });

后端controller接受请求

@RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity updateItem(Item item, @RequestParam("desc") String desc){
        try {
        
            this.itemService.updateItem(item,desc);
            //200
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("修改商品出错");
        }
        //500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    

控制台输出错误

2016-11-29 19:10:32,408 [http-bio-8080-exec-9] [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] DispatcherServlet with name 'taotao-manage' processing PUT request for [/rest/item]
2016-11-29 19:10:32,408 [http-bio-8080-exec-9] [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping]-[DEBUG] Looking up handler method for path /item
2016-11-29 19:10:32,408 [http-bio-8080-exec-9] [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver]-[DEBUG] Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2016-11-29 19:10:32,408 [http-bio-8080-exec-9] [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver]-[DEBUG] Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2016-11-29 19:10:32,408 [http-bio-8080-exec-9] [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver]-[DEBUG] Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2016-11-29 19:10:32,409 [http-bio-8080-exec-9] [org.springframework.web.servlet.PageNotFound]-[WARN] Request method 'PUT' not supported
2016-11-29 19:10:32,409 [http-bio-8080-exec-9] [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Null ModelAndView returned to DispatcherServlet with name 'taotao-manage': assuming HandlerAdapter completed request handling
2016-11-29 19:10:32,409 [http-bio-8080-exec-9] [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Successfully completed request

浏览器显示

Request URL:http://localhost:8080/rest/item
Request Method:PUT
Status Code:405 Method Not Allowed
Remote Address:[::1]:8080
3 个回答
  • 有可能是你的路径写错了(js 里面)
    参见:http://bbs.csdn.net/topics/39...

    4** 这样的异常一般是路径不对

    2022-10-26 23:15 回答
  • 应该是RequestMapping value没写

    2022-10-26 23:15 回答
  • 405 Method Not Allowed*:说明服务不存在。
    • 确定处理类没有缺少声明@Controller

    • 指定请求的实际地址:
      @RequestMapping(value = "/rest/item",method = RequestMethod.PUT)

    2022-10-26 23:15 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有