Home > Mobile >  Why its showing required request body in output
Why its showing required request body in output

Time:11-21

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";
}

Output:

{
  "timestamp": "2022-11-20T13:17:24.172 0000",
  "status": 400,
  "error": "Bad Request",
  "message": "Required request body is missing: public java.lang.String"
}

Please tell someone why its showing like this

CodePudding user response:

The @RequestBody annotation comes with the required attribute defaulting to true. This means that the request should always contain a body, otherwise it will throw an exception. From the error message it appears that your request does not contain a body.
You need to either set the required attribute to false or always provide a body.

CodePudding user response:

@Requestbody annotation requires you to pass some request body in json, map form. But you are just passing prodId. I think you should just change @RequestBody annotation.

(@Request Param prodId ,@Requestbody HHttpServletReques request)

  • Related