@ControllerAdvice не отлавливает исключение?
Здравствуйте нужна помощь. Есть кастомное исключение.
@Getter
@Setter
public class AuthenticationException extends RuntimeException{
private final HttpStatus httpStatus;
private final String message;
private final String value;
private final ErrorCode errorCode;
public AuthenticationException(String message, String value, HttpStatus httpStatus){
this.httpStatus = httpStatus;
this.message = message;
this.value = value;
this.errorCode = null;
}
public AuthenticationException(String message, String value, HttpStatus httpStatus, ErrorCode errorCode){
this.httpStatus = httpStatus;
this.message = message;
this.value = value;
this.errorCode = errorCode;
}
public AuthenticationException(String message, HttpStatus httpStatus){
this.httpStatus = httpStatus;
this.message = message;
this.value = null;
this.errorCode = null;
}
}
также есть место где это исключение выбрасывается
public ProfileSessionDTO getSessionBySessionId(String sessionId){
if (StringUtils.isEmpty(sessionId)) {
throw new AuthenticationException("Хэдер сессии не может быть пустым", HttpStatus.BAD_REQUEST);
}
return authenticationClient.sessionBySessionId(sessionId);
}
есть перехватчик исключений. При вызове этого исключения, в ответ приходит код ошибки 500 с моим сообщением. Хотя я жду 400 код ошибки. Я пробовал Ловить все исключения. @ExceptionHandler(Exception.class). Но программа не попадает в мой ApplicationExceptionHandler
@ControllerAdvice
public class ApplicationExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(AuthenticationException.class)
public ResponseEntity<AuthenticationExceptionResponseDTO> authenticationExceptionHandler(AuthenticationException e, HttpServletRequest request) {
AuthenticationExceptionResponseDTO response = createErrorResponse(e.getHttpStatus(), e.getMessage(), request.getRequestURI(), e.getValue());
response.setErrorCode(e.getErrorCode());
return new ResponseEntity<>(response, e.getHttpStatus());
}
}
Я смотрел много информации и много попробовал.
Пока я не могу решить свою задачу.
Варианты которые я пробовал
- @ExceptionHandler(Exception.class)
@Bean
public ErrorPageFilter errorPageFilter() {
return new ErrorPageFilter();
}
@Bean
public FilterRegistrationBean disableSpringBootErrorFilter(ErrorPageFilter filter) {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(filter);
filterRegistrationBean.setEnabled(false);
return filterRegistrationBean;
}
3.@Order(Ordered.HIGHEST_PRECEDENCE)
Я буду рад любой помощи.
Пример ответа
{
"timestamp": "2020-09-28T13:13:17.342+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Хэдер сессии не может быть пустым",
"path": "/ses"
}
Ответы (1 шт):
AuthenticationException такое исключение есть у Spring Security в пакете org.springframework.security.core. Проверьте импорты как минимум. В дебаг режиме, программа входит в необходимые контрольные точки? Если есть возможность, предоставьте ссылку на репозиторий, чтобы взглянуть более подробно.