Oct 21, 2014

Logging REST Exceptions with Spring

To enable logging for REST errors in Spring when using a ResponseEntityExceptionHandler just enable debug on ExceptionHandlerExceptionResolver

Without setting debug to true, your errors will still be handled perfectly but you will not receive any messages in your server logs about the handled error.

Of course, there’s also a complicated way to register an extended ExceptionHandlerExceptionResolver and set the warnLogCategory. But that is too much work when you can just enable debug logging to get a nice message like this:

DEBUG 15 Oct 2014 09:03:58,391 (AbstractHandlerExceptionResolver.java:134) – Resolving exception from handler [null]: org.springframework.web.bind.UnsatisfiedServletRequestParameterException: Parameter conditions “startDay” not met for actual request parameters:

And if you aren’t already using it, you can easily handle any REST exceptions from your Spring Controllers with a @ControllerAdvice annotated class that extends ResponseEntityExceptionHandler. Here is an example RestResponseEntityExceptionHandler that I use:

About the Author

Jeff Sheets profile.

Jeff Sheets

VP - Technology

Jeff has developed Java, Groovy, Grails, and Javascript web apps for industries as varied as Defense, Energy, Weather, Insurance, and Telecom. He is a co-organizer of the Omaha Java Users Group. Jeff has worked on Grails projects since the Grails 1.3.x days, and has experience with production Groovy code as well as Spock tests and Gradle builds. His latest focus has been on AngularJS and Spring Boot applications using JHipster. Jeff also enjoys volunteering at local CoderDojo events to teach programming to our next generation.

One thought on “Logging REST Exceptions with Spring

  1. Wim Deblauwe says:

    What is “ExceptionMessage” ? Is this your own class?

  2. Jeff Sheets says:

    Yes, I forgot to remove ExceptionMessage pieces before posting. It is just a simple wrapper class that we used on that project.

Leave a Reply to Wim Deblauwe Cancel reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
TICK Stack Monitoring for the Non-Technical
TICK – Telegraf, Influx, Chronograf, and Kapacitor – is a method of monitoring your systems and applications. In this article, I discuss in non-technical terms what the difference is between TICK and Prometheus Grafana A […]
Design Systems, Part 1 • Introduction
Business leaders need a practical guide to plan and execute Design System Initiatives. The aim of this series is to be that guide. This installment introduces terms and definitions as a primer on Design Systems.
ML for Translating Dysarthria Speech (Pre-Part 1)
What is Dysarthria? Per the Mayo Clinic, Dysarthria occurs when the muscles you use for speech are weak or you have difficulty controlling them. Dysarthria often causes slurred or slow speech that can be difficult […]
Develop Quality Code
As software continues to dominate every facet of our lives, developers are faced with an ever-increasing pressure to produce bug free code. The responsibility of clean quality software falls upon everyone that is involved in […]