Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

265
Vistas
Spring boot - setting up a global error handler to shield REST endpoints

I have a spring boot application with restful endpoints. I want to have a global error handler that basically traps any exceptions so that I can then manage the response (i.e. dont want to expose a big java stacktrace in the response)

Whats the best way to achieve this?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Create a controller advice class with an exception handler that will cache all exceptions of your desired type.

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public void handleAll(Exception e) {
        log.error("Unhandled exception occurred", e);
    }

}

If you like, you can return a body from the method just like from a regular controller method or simply return some HTTP status code. The above code returns 500 using the @ResponseStatus annotation.

You can catch only specific exception types. The @ExceptionHandler annotation accepts an array of exceptions as the value attribute.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda