【问题标题】:Why its showing required request body in output为什么它在输出中显示所需的请求正文
【发布时间】:2022-11-20 21:57:58
【问题描述】:
public String deleteProduct(@RequestBody String prodId ,HttpServletRequest request ) throws NotLoggedInException {
    
    String userName = (String) request.getSession().getAttribute("user");
    System.out.println(userName);
    if (userName == null) {
        throw new NotLoggedInException("You have not logged in");
    }
    String userRole = (String) request.getSession().getAttribute("role");
    if (!userRole.equalsIgnoreCase("productmaster")) {
        throw new AuthorizedUserRoleNotFoundException("you are not authorized to add the products");
    }
    if(pservice.deleteProduct(prodId))
    {
    return "Product deleted";
        
    }
    return "Product not deleted";
}

输出: { “时间戳”:“2022-11-20T13:17:24.172+0000”, “状态”:400, “错误”:“错误的请求”, “消息”:“缺少必需的请求正文:public java.lang.String }

请告诉别人为什么会这样显示

【问题讨论】:

  • 你如何称呼这个东西?你认为错误消息试图告诉你什么?

标签: java spring spring-boot


【解决方案1】:

@RequestBody 注释带有默认为 truerequired 属性。这意味着请求应该总是包含一个主体,否则会抛出异常。从错误消息看来,您的请求不包含正文。
您需要将 required 属性设置为 false 或始终提供正文。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 2022-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多