I am creating a standalone java application, application will be used by novice users, we will be sharing a set of error codes for trouble shooting the application. The requirement is to customize the error message, if the spring boot application failed to start with errors.
2025-01-02 15:33:14 - [] - [WARN] - [org.springframework.context.support.AbstractApplicationContext.refresh] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'
2025-01-02 15:33:14 - [] - [INFO] - [org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLogger.logMessage] -
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2025-01-02 15:33:14 - [] - [ERROR] - [org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter.report] -
***************************
APPLICATION FAILED TO START
***************************
Description:
Web server failed to start. Port 7000 was already in use.
Action:
Identify and stop the process that's listening on port 7000 or configure this application to listen on another port.
I tried using ControllerAdvice but didn't worked out.
@ControllerAdvice
public class GenericExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GenericExceptionHandler.class);
@ExceptionHandler(ApplicationContextException.class)
public void handleApplicationContextException(ApplicationContextException exception) {
log.error("Port Already in use, Refer Step 3.1 for configuring new port");
}
}
I am creating a standalone java application, application will be used by novice users, we will be sharing a set of error codes for trouble shooting the application. The requirement is to customize the error message, if the spring boot application failed to start with errors.
2025-01-02 15:33:14 - [] - [WARN] - [org.springframework.context.support.AbstractApplicationContext.refresh] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'
2025-01-02 15:33:14 - [] - [INFO] - [org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLogger.logMessage] -
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2025-01-02 15:33:14 - [] - [ERROR] - [org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter.report] -
***************************
APPLICATION FAILED TO START
***************************
Description:
Web server failed to start. Port 7000 was already in use.
Action:
Identify and stop the process that's listening on port 7000 or configure this application to listen on another port.
I tried using ControllerAdvice but didn't worked out.
@ControllerAdvice
public class GenericExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GenericExceptionHandler.class);
@ExceptionHandler(ApplicationContextException.class)
public void handleApplicationContextException(ApplicationContextException exception) {
log.error("Port Already in use, Refer Step 3.1 for configuring new port");
}
}
We can listen for the ApplicationFailedEvent for dealing with Exceptions occurring during the startup of spring boot application. Attaching the sample code how i dealt with same.
@Component
public class PortConflictListener implements ApplicationListener<ApplicationFailedEvent> {
private static final Logger log = LoggerFactory.getLogger(PortConflictListener.class);
@Value("${server.port}")
private String serverPort;
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
Throwable exception = event.getException();
if (exception instanceof ApplicationContextException
&& exception.getCause() instanceof PortInUseException) {
log.error("custom error message");
System.exit(1);
}
}
}
Based on spring documentation @ControllerAdvice is annotated with @Component which means it will be scanned by Spring Boot normally.
So, let's focus on your calss implementation, I see you are depending on the @ExceptionHandler(ApplicationContextException.class) although you want to listen to application failed event, right? I have good news ! there is something ready to use already provided by springboot, the ApplicationFailedEvent.
All what you need is just to create a custom expetion handler that listen for the ApplicationFailedEvent, I have tweaked you code submitted above to work as the explanasion above:
@ControllerAdvice
public class GenericExceptionHandler implements ApplicationListener<ApplicationFailedEvent> {
private static final Logger log = LoggerFactory.getLogger(GenericExceptionHandler.class);
@Value("${server.port}")
private String serverPort;
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
Throwable exception = event.getException();
if (exception instanceof ApplicationContextException
&& exception.getCause() instanceof PortInUseException) {
log.error("Port Already in use, Refer Step 3.1 for configuring new port");
System.exit(1);
}
}
}