0

My Spring boot app is standalone application which don't have controller. I am calling service layer from main method of spring boot application.

I have tried to use @ExceptionHandler @ControllerAdvice annotations in my class as below. but control never comes to My Exception Handler method

package com.test.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

    @ControllerAdvice
    public class MyExceptionHandler {
    
        @ExceptionHandler(NullPointerException.class)
        public void handleNullpointerExcetion() {
            System.out.println("Handling Null pointer exception");
        }
    }

Tried with package name as well which i need to scan in @ControllerAdvice but it is not working

@ControllerAdvice("com.test.utility")
    public class MyExceptionHandler {
    
        @ExceptionHandler(NullPointerException.class)
        public void handleNullpointerExcetion() {
            System.out.println("Handling Null pointer exception");
        }
    }

Are we not able to handle exception at centralized place if i we don't have controller class which we annotate with @RestContoller or @Controller

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
andrew
  • 79
  • 9

1 Answers1

-1

I don't think so.

ControllerAdvice is only used whenever a controller would return an exception, It is a layer that exists after your endpoint in the controller returns and before actually returning the response, to interject and globally handle the exception and is meant to be global only for controllers. https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/ControllerAdvice.html

since you don't have a controller, I'd imagine the ControllerAdvice will not help you there.

If you want a centralized error handling mechanism, you'd have to implement something similar to the controller advice yourself around your "Main" function, to which you can have unified exception handling responses.

  • ok.. so we can't use centralized exception handling with@controllerAdvice and@ExceptionHandler for application which don't have API exposed/ controller class @Hesham Amer – andrew Mar 31 '21 at 14:35
  • Arround the Main method? This will not be called when you start it as an executable JAR. – Simon Martinelli Mar 31 '21 at 14:39
  • @Simon, I thought the execution of the JAR is the execution of the Main function. I meant something like this `static void main(String[] args){ try { //Main code here } catch ( all your exceptions and handling ) { } } ` Would this not execute as a JAR? – Hesham Amer Apr 06 '21 at 09:55
  • The problem is that your application may be multi threaded and then the catch will not be executed. You should create an aspect for your service classes – Simon Martinelli Apr 06 '21 at 11:02
  • I'm unsure about that Multithreading part either @Simon, I haven't heard anything from the original question regarding multithreading. In my opinion, unless there's some unhandled threading happening there, all exceptions will be caught. I've done some reading regarding Aspect annotation, and it seems that it can be also a very good idea. Thanks for bringing attention to it. Link for Reference: https://docs.spring.io/spring-framework/docs/4.3.15.RELEASE/spring-framework-reference/html/aop.html – Hesham Amer Apr 06 '21 at 11:37