[英]Exception handling in Spring Boot Endpoints
我正在嘗試在具有SOAP端點和Rest控制器的Spring Boot應用程序中處理異常。
捕獲在rest控制器中發生的異常非常簡單,我只是使用@controlleradvice設置了一個具有@exceptionhandler方法的類,並且捕獲了所有異常。 但是,此controlleradvice似乎沒有捕獲SOAP端點中發生的異常。 有沒有一種方法可以捕獲@controlleradvice類在端點中引發的異常? 如果不是,是否有其他方法可以將異常處理集中到整個應用程序中,而與引發異常的位置無關?
提前非常感謝您。
我們可以創建自定義的異常:
@ResponseStatus(HttpStatus.NOT_FOUND) public class StudentNotFoundException extends
RuntimeException {}
Spring中的異常處理程序:
@ControllerAdvice
@RestController
public class CustomizedResponseEntityExceptionHandler extends
ResponseEntityExceptionHandler {
@ExceptionHandler(StudentNotFoundException.class)
public final ResponseEntity<ErrorDetails>
handleUserNotFoundException(StudentNotFoundException ex, WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(),
request.getDescription(false));
return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
}
public class ErrorDetails {
private Date timestamp;
private String message;
private String details;
public ErrorDetails(Date timestamp, String message, String details) {
super();
this.timestamp = timestamp;
this.message = message;
this.details = details;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.