I have a Spring Boot 3 application with a global exception handler that works perfectly when I run it locally, but when I deploy the same code to Render, the exception handling stops working — I only get a generic 500 Internal Server Error response.
Here’s my global exception handler:
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(DataConflictException.class)
public ResponseEntity<Object> handleDataConflict(DataConflictException ex) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("timestamp", LocalDateTime.now());
body.put("status", HttpStatus.CONFLICT.value());
body.put("error", "Data Conflict");
body.put("message", ex.getMessage());
return new ResponseEntity<>(body, HttpStatus.CONFLICT);
}
@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<Object> handleDataIntegrityViolation(DataIntegrityViolationException ex) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("timestamp", LocalDateTime.now());
body.put("status", HttpStatus.CONFLICT.value());
body.put("error", "Data Conflict");
body.put("message", ex.getMostSpecificCause().getMessage());
return new ResponseEntity<>(body, HttpStatus.CONFLICT);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleGenericException(Exception ex) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("timestamp", LocalDateTime.now());
body.put("status", HttpStatus.INTERNAL_SERVER_ERROR.value());
body.put("error", "Internal Error");
body.put("message", ex.getMessage());
return new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
When I run the application locally (using IntelliJ), if a duplicate key is inserted into the database (PostgreSQL), I get a proper JSON response like this:
{
"timestamp": "...",
"status": 409,
"error": "Data Conflict",
"message": "This email has already been registered"
}
However, when I deploy the exact same code to Render, I only get this response instead:
{
"timestamp": "...",
"status": 500,
"error": "Internal Error",
"message": "could not execute statement [ERROR: duplicate key value violates unique constraint ...]"
}
It looks like my @RestControllerAdvice isn’t being triggered
My project structure is like this:
com.salus.agenda
├── SalusApplication.java (@SpringBootApplication)
├── Configuration
│ ├── SecurityConfiguration.java
│ └── GlobalExceptionHandler.java
├── Controllers
├── Services
└── Repositories
I’ve confirmed that everything works locally.
Is there any reason why the @RestControllerAdvice might not be registered or triggered when running on Render?
Could it be related to Spring profiles, filters (like my JWT filter), or database-level exceptions happening before the controller layer?
Any insights or troubleshooting tips would be appreciated.
@SpringBootApplicationannotated class look like? There is too little information here.