2

I have a spring boot application that run on jetty server. the requirement is to customize the 404 and other errors with our own messages. After searching for a while got to a point that we can do it by a custom class by extending the ErrorHandler class of jetty package and override the method getErrorPage(HttpServletRequest request).

doubt: was not able to figure out how to configure this customized class in spring boot application to over ride the default ErrorHandler class. Is there a way to inject this custom class?

Any help/suggestion would be really helpful.

Ghost Rider
  • 688
  • 3
  • 17
  • 38
  • If you are using Servlets, just make an error-page handler in your webapp. - see https://stackoverflow.com/questions/7066192/how-to-specify-the-default-error-page-in-web-xml – Joakim Erdfelt Feb 08 '18 at 20:10
  • Possible duplicate of https://stackoverflow.com/questions/37398385/spring-boot-and-custom-404-error-page – Joakim Erdfelt Feb 08 '18 at 20:11
  • If you just want to modify the content of the default JSON response returned by Spring Boot on _embedded_ Jetty, this would be the easiest way to go: https://stackoverflow.com/questions/29106637/modify-default-json-error-response-from-spring-boot-rest-controller#29109756 – Janaka Bandara Mar 03 '20 at 14:08

1 Answers1

0

I am able to achieve this custom error handling by creating a custom class by extending the

org.eclipse.jetty.server.handler.ErrorHandler

class( as mentioned in my question) and overriding the doError method.

this custom class needs to configured as an error handler class while initiating the jetty server as below.

org.eclipse.jetty.server.Server server = new Server();
server.setErrorHandler(new CustomErrorHandler());

As a result when any wrong URL is sent to your application then your custom error handler class method will be called where you can write your own custom response body/content.

Ghost Rider
  • 688
  • 3
  • 17
  • 38