【问题标题】:Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter缺少所需的请求正文内容:org.springframework.web.method.HandlerMethod$HandlerMethodParameter
【发布时间】:2015-05-27 05:39:51
【问题描述】:

将 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:不支持请求方法“POST”

【问题讨论】:

  • 您是否会发布 AjaxResponse 类的代码,您的 url 也是“/BusinessReimbursment/addDepartment”,但您请求的映射只是“addDepartment”,应该是“/addDepartment”还是“/BusinessReimbursment/addDepartment” "。
  • 网址没有问题。因为它可以在没有 requestBody 的情况下工作。
  • public class AjaxResponse { private boolean success; private String message; public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }

标签: java ajax json jackson spring-4


【解决方案1】:

您不应发送带有 HTTP GET 请求的请求正文。您应该修改 addDepartment() 使其仅支持 POST,并将您的 JSON 发布到该端点。如果您想获取有关部门的信息,您应该创建一个单独的控制器方法来执行此操作(并且不需要请求正文)。

此外,请仔细检查您的端点定义,因为您在 $.ajax 调用中拼错了“reimbursement”。

【讨论】:

  • 我在请求映射注释中提到了 GET n POST。如果我通过帖子回复,那么它会给出我在帖子中提到的错误..
  • 请再次阅读我的回答。您不能在 @RequestMapping 注释中使用 GET@RequestBody,因为 GET 请求不能有正文。一般来说,GET 应该只用于只读操作,例如获取记录或搜索。完全删除 GET 并重试。
  • 我更改了方法:POST,并从 @requestMapping 注释中删除了 RequestMethod:GET 我收到以下错误,我在帖子中提到了。
  • Request method 'POST' not supported Invalid CSRF token found for http://localhost:8080/BusinessReimbursment/addDepartment DispatcherServlet with name 'dispatcher' processing POST request for [/BusinessReimbursment/403] Looking up handler method for path /403 HttpRequestMethodNotSupportedException: Request method 'POST' not supported HttpRequestMethodNotSupportedException: Request method 'POST' not supported HttpRequestMethodNotSupportedException: Request method 'POST' not supported WARN o.s.web.servlet.PageNotFound - Request method 'POST' not supported
  • Invalid CSRF token 是一个完全不同的问题。
【解决方案2】:

我也遇到了同样的问题。我对 JSON 请求使用“邮递员”。代码本身没有错。我只是将内容类型设置为 JSON (application/json) 就可以了,如下图所示

【讨论】:

  • 这是一个问题吗?如果是这样,post it as a separate question。如有必要,您可以添加此问题的链接。
  • 下次不要把整篇文章都变成链接,最好在最后加上链接。此外,OP 没有提及 Postman。如果您想解决此问题,最好显示具体错误是什么以及如何重现错误。
【解决方案3】:

试试这个:

@RequestBody(required = false) 字符串字符串

【讨论】:

  • 不,这可能无济于事
  • 我不明白为什么这会奏效,但它确实奏效了,而且我的 requestbody 经历了之前它没有...
  • @Juan 这与根本不使用注释相同,它仍然可以工作,而不是解决方案
【解决方案4】:

我对您的代码进行了一些小的修改,并使用我拥有的 spring 项目对其进行了测试,并且它可以正常工作,如果我使用 GET,该逻辑将仅适用于 POST,它会抛出无效请求的错误。同样在您的 ajax 调用中,我注释掉了 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;
  }

【讨论】:

    【解决方案5】:

    对不起,伙计们..实际上是因为需要一个 csrf 令牌,所以我遇到了这个问题。 我已经实现了 spring security 并且 csrf 是启用的。并且通过 ajax 调用我需要传递 csrf 令牌。

    【讨论】:

    • Request method 'POST' not supported Invalid CSRF token found for http://localhost:8080/BusinessReimbursment/addDepartment DispatcherServlet with name 'dispatcher' processing POST request for [/BusinessReimbursment/403] Looking up handler method for path /403 HttpRequestMethodNotSupportedException: Request method 'POST' not supported HttpRequestMethodNotSupportedException: Request method 'POST' not supported HttpRequestMethodNotSupportedException: Request method 'POST' not supported WARN o.s.web.servlet.PageNotFound - Request method 'POST' not supported –
    【解决方案6】:

    在我这边,这是因为POSTMAN设置问题,但我不知道为什么,也许我从其他人那里复制了一个查询。我只需在 POSTMAN 中创建一个新请求并运行它即可。

    【讨论】:

      【解决方案7】:

      如果它在 Postman 中工作,请尝试新的 Spring 版本,因为 'org.springframework.boot' 2.2.2.RELEASE 版本可能会抛出“缺少必需的请求正文内容”异常。
      试试 2.2.6.RELEASE 版本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-09
        • 1970-01-01
        • 2017-08-01
        • 2019-10-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-04
        • 2020-03-24
        相关资源
        最近更新 更多