I am using spring mvc, to handle excpetion i use global exception handler
@ControllerAdvice
public class GlobalControllerExceptionHandler {
@ResponseStatus(value = HttpStatus.CONFLICT, reason = "Data integrity violation")
@ExceptionHandler({DataIntegrityViolationException.class})
public @ResponseBody AdminResponse handleConflict(DataIntegrityViolationException ex,HttpServletResponse httpServletResponse) {
AdminResponse error = new AdminResponse ();
httpServletResponse.setStatus(HttpStatus.CONFLICT.value());
error.setStatus(Status.FAILURE);
error.setErrorDescription(ex.getMessage());
return error;
}
as i know, the annotation @ResponseStatus(value = HttpStatus.CONFLICT will change the repose status code into HttpStatus.CONFLICT, but that is not happen.
when i created dummy exception and annotated this dummy exception with @ResponseStatus then throw this new exception, the GlobalControllerExceptionHandler catches and handle the exception and also changes the response status code.
how can i change the response status code without creating new Exception, i just need to catch DataIntegrityViolationException