json 缺少所需的请求正文内容:org.springframework.web.method.HandlerMethod$HandlerMethodParameter

fiei3ece  于 2023-11-20  发布在  Spring
关注(0)|答案(7)|浏览(133)

将JSON数据从JSP传递到ResponseBody中的控制器时出错。

07:13:53.919 DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public com.chaitanya.ajax.AjaxResponse com.chaitanya.web.controller.DepartmentController.addDepartment(com.chaitanya.ajax.AjaxResponse)]: 

org.springframework.http.converter.HttpMessageNotReadableException: Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter@98d8d36c
07:13:54.106 DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [public com.chaitanya.ajax.AjaxResponse com.chaitanya.web.controller.DepartmentController.addDepartment(com.chaitanya.ajax.AjaxResponse)]: org.springframework.http.converter.HttpMessageNotReadableException: Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter@98d8d36c
07:13:54.125 DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [public com.chaitanya.ajax.AjaxResponse com.chaitanya.web.controller.DepartmentController.addDepartment(com.chaitanya.ajax.AjaxResponse)]: org.springframework.http.converter.HttpMessageNotReadableException: Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter@98d8d36c
07:1

字符串
Ajax调用:

$.ajax({ 
                        url: "/BusinessReimbursment/addDepartment", 
                        method: 'POST', 
                        dataType: 'json', 
                        data: "{\"message\":\"abc\",\"success\":true}",
                        contentType: 'application/json',
                        mimeType: 'application/json',
                        success: function(data) { 
                            alert(data.id + " " + data.name);
                            commit(true);
                        },
                        error:function(data,status,er) { 
                            alert("error: "+data+" status: "+status+" er:"+er);
                        }
                    });


控制器:

@RestController
public class DepartmentController {

    @Autowired 
    @Qualifier("departmentService")
    private DepartmentService departmentService;

    @RequestMapping(value="/addDepartment", method={RequestMethod.POST})
    public @ResponseBody AjaxResponse addDepartment(@RequestBody AjaxResponse  departmentDTO){
        AjaxResponse response=new AjaxResponse();
        return response;
    }


AppConfig.java
@豆豆

public RequestMappingHandlerAdapter  annotationMethodHandlerAdapter()
{
    final RequestMappingHandlerAdapter annotationMethodHandlerAdapter = new RequestMappingHandlerAdapter();
    final MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter = new MappingJackson2HttpMessageConverter();

    List<HttpMessageConverter<?>> httpMessageConverter = new ArrayList<HttpMessageConverter<?>>();
    httpMessageConverter.add(mappingJacksonHttpMessageConverter);

    String[] supportedHttpMethods = { "POST", "GET", "HEAD" };

    annotationMethodHandlerAdapter.setMessageConverters(httpMessageConverter);
    annotationMethodHandlerAdapter.setSupportedMethods(supportedHttpMethods);

    return annotationMethodHandlerAdapter;
}


请帮助我摆脱这种情况。我使用Spring 4,jakson 2.3.0
如果我尝试POST请求,它会给出:org.springframework.web.HttpRequestMethodNotSupportedException:Request method 'POST' not supported

irlmq6kh

irlmq6kh1#

你不应该在一个HTTP GET请求中发送一个请求体。你应该修改addDepartment(),使它只支持POST,并将你的JSON POST到那个端点。如果你想获取一个部门的信息,你应该创建一个单独的控制器方法来做这件事(并且不需要请求体)。
另外,请仔细检查端点定义,因为您在$.ajax调用中拼错了“recommendation”。

wlp8pajw

wlp8pajw2#

我也遇到了同样的问题。我使用“Postman”作为JSON请求。代码本身并没有错。我只是将内容类型设置为JSON(application/json),它就工作了,正如你在下面的图片中看到的那样


的数据

pgvzfuti

pgvzfuti3#

试试这个:
@RequestBody(required = false)String str

dfddblmv

dfddblmv4#

我对你的代码做了一些小的修改,并用我拥有的spring项目测试了它,它工作正常,如果我使用GET,它只会与POST一起工作,它会抛出一个带有无效请求的错误。此外,在你的aExcitCall中,我注解掉了commit(true),浏览器调试器标记和错误,它没有定义。只需修改URL以适应你的Spring项目架构。

$.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;
  }

字符串

thtygnil

thtygnil5#

对不起,伙计们..实际上是因为需要一个csrf令牌,我得到了这个问题。我已经实现了Spring Security 和csrf是启用的。通过aerupon调用,我需要传递csrf令牌。

camsedfj

camsedfj6#

在我这边,这是因为POSTMAN设置问题,但我不知道为什么,也许我复制了一个查询从其他。我只是创建一个新的请求在POSTMAN和运行它,它的工作。

qnzebej0

qnzebej07#

如果它在Postman中工作,请尝试新的Spring版本,因为'org.springframework. Boot ' 2.2.2.RELEASE版本可能会抛出“Required request body content is missing”异常。
请尝试2.2.6.RELEASE版本。

相关问题