M Chen
M Chen

Reputation: 133

Spring controller Required String parameter is not present

I wrote a request(method:delete) in my ajax. I used a deleteMapping in my controller. And when I trigger this request, I got a 400 error. But in browser I can see the data projectIdenter image description here

The console said Required String parameter 'projectId' is not present

Total log

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

Here is the 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
                },              
            });
         }
    } 

})

and my controller

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

Where is the problem?

Upvotes: 0

Views: 2695

Answers (1)

Barath
Barath

Reputation: 5283

As per the commented link below ,

If a DELETE request includes an entity body, the body is ignored

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

            });
         }
    } 

})

Upvotes: 4

Related Questions