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

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?

Related

Spring Boot How can i easy Scope Request bean in Async method?

Spring boot
How can i easy use bean with Request Scope in async method.
When i try execute got exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean ! class': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
I found a simple solution to this error.
I've initialized the requestScope bean.
which contains #Async method.
Now every request(rest) call with controller receives a new instance of bean.

how to customized exception handler in spring boot and jersey

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!

Catch spring transaction exception in service

i'd like to catch spring transaction exception in service layer not in the service heighr layer or the service caller.
As i found i can not catch the exception in the #transaction method.i need to take an action once the the transaction failes in the same method or the same service.
Transaction is rolled back when exception is thrown from method annotated with #Transactional (or class can be annotated with #Transactional).
So you can't do post rollback actions in this method. If you want to do this logic on service layer, you can have service bean handling post rollback action call another service bean which handles transaction.

spring Transaction exception handling

Basically with spring Transaction management .We can annotate the service layer class with #Transactional.
Also i have annotated my DAO layer class with #repository to make checked exception to unchecked exception.
Now when a runtimeexception is encoutered by the service layer class it will rollback.
My question how do we go about the exception handling. I will have to put some error message to UI .So i need to capture the exception .
How do i go about 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