我最近在我的 API Rest 中實作了身份驗證,但是當用戶嘗試訪問 api 而沒有被授權時,不會出現例外訊息:這是我的 GlobalExceptionHandler ,其方法例外涵蓋了 api 中的所有錯誤:
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorDetails> handlerExceptionMethod(Exception ex , WebRequest
webRequest){
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(),
webRequest.getDescription(false));
return new ResponseEntity<ErrorDetails>(errorDetails,
HttpStatus.INTERNAL_SERVER_ERROR);
}
Postman 軟體回傳 1,我想查看 errorDetails 引數。這是我的 ErrorDetails 類:
@Getter
public class ErrorDetails {
private Date timestamp;
private String message;
private String details;
public ErrorDetails(Date timestamp, String message, String details) {
this.timestamp = timestamp;
this.message = message;
this.details = details;
}
}
uj5u.com熱心網友回復:
我通過在 Security CONfig 類中添加以下代碼行解決了這個問題:
.antMatchers("/error").permitAll()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/489156.html
