Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter

前端 未结 7 1102
醉梦人生
醉梦人生 2021-01-17 07:37

Error to Pass JSON data from JSP to controller in ResponseBody.

07:13:53.919 DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from         


        
7条回答
  •  花落未央
    2021-01-17 08:26

    I made some minor modification to you code and tested it with a spring project that I have and it works, The logic will only work with POST if I use GET it throws an error with invalid request. Also in your ajax call I commented out commit(true), the browser debugger flagged and error that it is not defined. Just modify the url to fit your Spring project architecture.

     $.ajax({ 
                        url: "/addDepartment", 
                        method: 'POST', 
                        dataType: 'json', 
                        data: "{\"message\":\"abc\",\"success\":true}",
                        contentType: 'application/json',
                        mimeType: 'application/json',
                        success: function(data) { 
                            alert(data.success + " " + data.message);
                            //commit(true);
                        },
                        error:function(data,status,er) { 
                            alert("error: "+data+" status: "+status+" er:"+er);
                        }
                    });
    
    
    
    @RequestMapping(value="/addDepartment", method=RequestMethod.POST)
      public AjaxResponse addDepartment(@RequestBody final AjaxResponse  departmentDTO)
      {
        System.out.println("addDepartment: >>>>>>> "+departmentDTO);
        AjaxResponse response=new AjaxResponse();
        response.setSuccess(departmentDTO.isSuccess());
        response.setMessage(departmentDTO.getMessage());
        return response;
      }
    

提交回复
热议问题