I was trying to handle global exception but its not working without method call from REST API.
I have following code
@SpringBootApplication
public class LogReduceDemoApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(LogReduceDemoApplication.class, args);
System.out.println(".......Started......");
LogReduceDemoApplication.testException();
}
public static void testException() throws Exception {
throw new Exception("testException...");
}
}
Exception Handler
@ControllerAdvice
public class ExceptionHelper {
static final Logger logger = LoggerFactory.getLogger(ExceptionHelper.class.getName());
@ExceptionHandler(value = { NullPointerException.class,Exception.class })
public ResponseEntity<Object> handleException(Exception ex) {
System.out.println("Inside handleException...");
String msg="ex="+ex+", ex.getMessage()="+ex.getMessage();
System.out.println("Exception Msg: "+ msg);
return new ResponseEntity<Object>(msg, HttpStatus.BAD_REQUEST);
}
}
When I am calling LogReduceDemoApplication.testException() method from REST controller then it is triggering exception handler. But when there is a call to the same method using main() function its not triggering Exception handler instead it is printing all exception details.
How to use exception handler for the method call from main function(not from REST controller)?