java.lang.IllegalStateException:提交响应后无法调用sendError()
如上所述,我在行响应.sendError()方法中遇到错误。我已经在互联网上搜索了4天。但是我无法弄清楚问题所在。这个问题可能是什么原因?
我有一个自定义的异常类。在以下代码中,catch块成功捕获了我的自定义异常。但是,当我尝试将其发送到客户端时,它将返回上述异常。
我已经尝试过解决方案1 –控制器级别@ExceptionHandler解决方案2 – HandlerExceptionResolver解决方案3 – @ControllerAdvice解决方案4 – ResponseStatusException
解决上述问题的方法。但是对我不起作用:(
我已将错误代码和错误消息添加到响应正文。然后,我可以成功获取错误。但这不是我想要的方式。
@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; }
我需要使用上述sendError()方法获取错误代码和错误消息作为JSON响应。异常成功进入catch块。
@RestController @CrossOrigin(origins = "*") public class MemberController { @Autowired MemberInfoService memberinfoservice; @RequestMapping(value="/getMemberID/", method=RequestMethod.POST ,headers = "Accept=application/json") public ResponseMemberID getMemberID(@RequestBody RequestMemberID request) throws Exception{ Member_info memberinfo = new Member_info(); ResponseMemberID _response =new ResponseMemberID(); memberinfo = memberinfoservice.getMemberID(request.getPin(),request.getMsisdn()); if(memberinfo == null) { throw new PINNotValidException(); } _response.setMember_id(memberinfo.getMember_id()); return _response; } @ExceptionHandler(PINNotValidException.class) public ResponseEntity<ErrorResponse> exceptionHandler(Exception ex) { ErrorResponse error = new ErrorResponse(); error.setErrorCode(PSAExceptionCodes.PIN_VALIDATION_FAIL); error.setMessage(ex.getMessage()); return new ResponseEntity<ErrorResponse>(error, HttpStatus.OK); }
上面提到的是我根据链接http://www.jcombat.com/spring/exception-handling-in-spring- restful-web-service更改的代码,它满足了我的问题。
此外,我还尝试在我创建的应用程序中使用 HttpServletResponse 类的 sendError() 方法,但该方法始终返回空响应。
但是通过在创建单独的 PINNotValidException.class , ErrorResponse.class , PSAException.class 和 PSAExceptionCodes.class 时使用@ExceptionHandler作为自定义异常,我可以轻松解决我的问题。下面分别提到了这些类。
PINNotValidException.class
public class PINNotValidException extends PSAException{ private static final long serialVersionUID = 1L; private static String errorMessage = "Pin Number and Mobile Number mismatch."; private static int errorCode = PSAExceptionCodes.PIN_VALIDATION_FAIL; public PINNotValidException() { super(errorMessage, errorCode); } }
ErrorResponse.class
public class ErrorResponse { private int errorCode; private String message; public int getErrorCode() { return errorCode; } public void setErrorCode(int errorCode) { this.errorCode = errorCode; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
PSAException.class
public class PSAException extends Exception { private static final long serialVersionUID= 100L; private String errorMessage; private int errorCode; public String getErrorMessage() { return errorMessage; } public int getErrorCode(){ return errorCode; } public PSAException(String errorMessage) { super(errorMessage); this.errorMessage = errorMessage; } public PSAException(String errorMessage, int errorCode) { super(errorMessage); this.errorMessage = errorMessage; this.errorCode=errorCode; } public PSAException() { super(); } }
PSAExceptionCodes.class
public class PSAExceptionCodes { public static final int PIN_VALIDATION_FAIL = 251; //Pin and mobile mismatch public static final int ACCOUNT_TYPE_NOT_FOUND = 300; //Account type mismatch public static final int NO_MEMBER_FOUND = 250; //Member Not Found public static final int MEMBER_NOT_REGISTERED = 256; //Member not registered }