Skip to content
Advertisement

How to catch non-MVC and non-REST exceptions in Spring Boot

I’ve been able to find endless amounts of tutorials on how to catch unhandled exceptions in Spring MVC or Spring REST, but what I want to know is how to catch unhandled exceptions without using the Spring Web framework at all.

I am writing an application which does not have a web component, and I am not going to import Spring Web only for exception handling.

When a @Service throws an exception that goes unhandled, I need to catch it so that I can log it properly to Raygun.

For example, consider this method in a Service that purposely throws an uncaught exception:

JavaScript

Its output will be:

JavaScript

How do I catch that?

Is there no easy way to do this?

Advertisement

Answer

You can define an aspect. Using Java-based configuration it will look like this:

JavaScript

If you need to inject a bean, add the @Component annotation:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement