Reputation: 141
I'm trying to handle exceptions in a spring boot application that has SOAP endpoints and Rest controllers.
Catching exceptions that occur in the rest controller is quite straightforward, I just set a class with @controlleradvice that has @exceptionhandler methods and all exceptions get caught. However, this controlleradvice doesn't seem to catch exceptions that occur in the SOAP endpoints. Is there a way to catch the exceptions that are thrown in the endpoints on a @controlleradvice class? If not, is there some other way to centralize exception handling throughout the entire application, independently from where the exceptions are thrown?
Thank you so much in advance.
Upvotes: 3
Views: 3086
Reputation: 9
We can create customized exception:
@ResponseStatus(HttpStatus.NOT_FOUND) public class StudentNotFoundException extends
RuntimeException {}
Exception handler in 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;
}
Upvotes: -1