Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!