Spring控制器必需的String参数不存在

时间:2017-08-14 04:35:40

标签: java ajax spring spring-boot

我在我的ajax中写了一个请求(方法:删除)。我在我的控制器中使用了deleteMapping。当我触发此请求时,我收到400错误。但在浏览器中,我可以看到数据 projectId enter image description here

控制台说Required String parameter 'projectId' is not present

总记录

2017-08-14 13:06:11.296  WARN 8584 --- [nio-8080-exec-3] o.s.web.servlet.PageNotFound             : Request method 'DELETE' not supported
2017-08-14 13:06:11.296  WARN 8584 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported

2017-08-14 12:32:57.239  WARN 8584 --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'projectId' is not present

这是 ajax

$('#delete-project-btn').on('click', function() {
    if (cur != null) {
        alert(cur);
        if(window.confirm('sure?')) {
            $.ajax({
                url : '/index/'+cur,
                type : 'delete',
                dataType : 'text',
                data : {
                    projectId : cur
                },              
            });
         }
    } 

})

和我的controller

     @DeleteMapping("/index/{projectId}")
     public String deleteProject(@PathVariable("projectId") int id) {
        System.out.println(id);
//        projectRepository.delete(Integer.valueOf(id));
        return "redirect:/index";
    }

问题出在哪里?

1 个答案:

答案 0 :(得分:4)

根据下面的评论链接,

  

如果DELETE请求包含实体主体,则忽略正文

$('#delete-project-btn').on('click', function() {
    if (cur != null) {
        alert(cur);
        if(window.confirm('sure?')) {
            $.ajax({
                url : '/index?projectId='+cur,
                type : 'delete',
                dataType : 'text'

            });
         }
    } 

})