java - 已经在web.xml中配置过滤器,为何无法提交put请求?
迷茫
迷茫 2017-04-18 10:23:48
0
3
535

web.xml中配置过滤器

    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

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<Void> 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
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

répondre à tous(3)
大家讲道理
405 Method Not Allowed*:说明服务不存在。
  • 确定处理类没有缺少声明@Controller

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

迷茫

应该是RequestMapping value没写

巴扎黑

有可能是你的路径写错了(js 里面)
参见:http://bbs.csdn.net/topics/39...

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

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!