how to customized exception handler in spring boot and jersey - spring-boot

I use spring boot and jersey,I want to handle this exception by myself, but spring has a handle exception method. I search method to customized exception handler, but it only suitable springmvc. #ControllerAdvice only match #RequestMapping. If you know how to cancel this autoconfiguration exception handler is also useful.Thank you very very much!

Related

How control advice catches exception

I am trying to understand how ControllerAdvice is working in SpringBoot. It is suggested that for per application there should be one ControllerAdvice. But my question is how this ControllerAdvice is tied to Controllers and catching the exceptions. So basically what is the underhood?
Spring AOP works with proxies.
That is when you annotate a class with any of AOP annotation spring will create a proxy class by extending your annotated class and all the methods will be overridden there in the proxy class.
Thus here after when you call a method in your class spring will call the proxy object method first then your actual method. This is Spring AOP knows whether the method has been called or thrown some exception or returned successfully etc etc.
This is the reason why when you call a private method with in the class AOP cannot intercept that method call.

Does Spring have any way of catching/handling ServletException?

I there any predefined way in Spring to catch or handle ServletException?
I mean any annotation or class that catches them and provide utility methods for them?
There are several techniques and approaches to handle Servlet Exceptions with Spring.
I can redirect you to this interesting link:
Exception Handling for REST with Spring
Exceptions can be managed at the DispatchServlet level, Controller level or application level. You can chose the method that better fit your needs.
Exception handling can be implemented per controller (using the old #ExceptionHandler or more recent and easy to use ResponseStatusException), or within global exception handler that centralize all exception management (using #ControllerAdvice)

Correct way to find if spring context was started

In my spring bean I want to use send spring event functionality. The problem is event can't be sent if spring context was not initialized and my bean by some reasons can send events before that happen.
I used the following:
implement ApplicationContextAware and use ConfigurableApplicationContext.isActive() - this becomes true in the beginning of the context initialization phase
use ConfigurableApplicationContext.isRunning() - this throws exception IllegalStateException("LifecycleProcessor not initialized...
listen for ContextRefreshedEvent - this doesn't work because this is inner bean and is used as a property for the bean that implements BeanFactoryPostProcessor
implementing SmartLifecycle also doesn't work because for inner beans
So what is the EASY and correct way to determine if context is running and event can be sent?
Hopefully this is already fixed SPR-13667 in spring 4.1.7.

Spring Boot service exception is not caught by #ControllerAdvice exception handler

I have all my api controller methods in a controller with an exception handler annotated with "ControllerAdvice". This way, I can always return a JSON response to the frontend even if an unexpected exception occurs. This works fine for exceptions raised by code in the controller, but for exceptions raised in code in a service method called by the controller, the exception handler isn't used.
I don't want to have to specifically annotate the service to use the same exception handler because it may be called by a controller that we use different exception handling for.
Is there a way to prevent the service from handling the exception itself and instead have the controller's exception handler handle it?

Graceful handling of exception thrown while creating beans

I want my Spring MVC web app to gracefully handle a particular type of exception thrown while creating the beans.
The construction of one of my beans reads configuration data from an external file. If that configuration data is faulty, one of my bean constructors will throw an exception of a particular type. As the cause of the problem would be a faulty configuration file, I want my web application to respond with a useful log message and/or an error page, rather than a stack-trace of the thrown exception. So I guess I need some kind of exception handler hooked into the IOC container or dispatcher servlet. How can I do that?
Just to be clear. I'm asking about exceptions thrown as the servlet is initialising, not as it handles HTTP requests, so #ExceptionHandler annotations on controllers are not useful.
I worked around this difficulty by introducing a level of indirection. My bean is really just a handle. If the configuration file is bad, the bean catches the exception, logs a message, and notes that reading failed. Accessing the bean later then throws a suitable exception.

Resources