java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
As above mentioned I am getting an error in the line response .sendError() method. I have searched the internet for 4 days. But I couldn't figure out the problem. What can be the reason for this problem?
I have a custom exception class. In the following code, the catch block is successfully caught my custom exception. But when I try to send it to the client side it returns the exception mentioned above.
I have already tried Solution 1 – The Controller level @ExceptionHandler Solution 2 – The HandlerExceptionResolver Solution 3 – @ControllerAdvice Solution 4 – ResponseStatusException
Ways to resolve the above issue. But non-worked for me :(
I have added the error code and the error message to the response body. Then I can get the error successfully. But that is not the way I want.
@RequestMapping(value="/getMemberID/", method=RequestMethod.POST ,headers ="Accept=application/json")
public ResponseMemberID getMemberID(@RequestBody RequestMemberID request,HttpServletResponse response, @Context HttpServletRequest _request ) throws Exception {
Member_info memberinfo = new Member_info();
ResponseMemberID _response =new ResponseMemberID();
try {
memberinfo = memberinfoservice.getMemberID(request.getPin(),request.getMsisdn());
_response.setMember_id(memberinfo.getMember_id());
}catch(MemberNotFoundException exp) {
response.sendError(exp.getErrorCode(), exp.getErrorMessage());
}
return _response;
}
I need to get the error code and error message as a JSON response using above sendError() method. The exception is successfully coming to the catch block.