<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: send2abhishek</title>
    <description>The latest articles on DEV Community by send2abhishek (@send2abhishek).</description>
    <link>https://dev.to/send2abhishek</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F682649%2Fd81e790b-1875-4c62-85e4-ac9c8e52497c.jpeg</url>
      <title>DEV Community: send2abhishek</title>
      <link>https://dev.to/send2abhishek</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/send2abhishek"/>
    <language>en</language>
    <item>
      <title>How to customise white-label error in spring boot </title>
      <dc:creator>send2abhishek</dc:creator>
      <pubDate>Sun, 02 Jan 2022 15:13:09 +0000</pubDate>
      <link>https://dev.to/send2abhishek/how-to-customise-white-label-error-in-spring-boot-672</link>
      <guid>https://dev.to/send2abhishek/how-to-customise-white-label-error-in-spring-boot-672</guid>
      <description>&lt;p&gt;Hi Guys in this post I am going to explain how to override the white-label error page in springboot application.&lt;/p&gt;

&lt;p&gt;Follow below steps to achieve this -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new class annotate with &lt;em&gt;@ControllerAvice&lt;/em&gt; and extend it with &lt;em&gt;ResponseEntityExceptionHandler&lt;/em&gt; (this is the central class that wish to provide centralized exception handling across all @RequestMapping methods through @ExceptionHandler methods).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@ControllerAdvice
public class GlobalExceptionHandler extends
ResponseEntityExceptionHandler {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;As white label error produces because of various reasons but in this post I am going to focus only on when there is no handler attached to the request sent by the client. In this case due to spring auto configuration route wired to the spring default error route which as a result shows the white label error page. So now we know the root cause lets customise this behaviour.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Override the method &lt;em&gt;handleNoHandlerFoundException&lt;/em&gt; from &lt;em&gt;ResponseEntityExceptionHandler&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; @Override
    protected ResponseEntity&amp;lt;Object&amp;gt; handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

 String path = ((ServletWebRequest) request).getRequest().getRequestURI();

return new ResponseEntity&amp;lt;&amp;gt;(new ErrorResponse(status, ex.getLocalizedMessage(), path, ex.getMessage()), status);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code simply I am returning a bean along with wiring the  root messages and path.&lt;br&gt;
Also I have used a custom pojo class to structure the information. Lets create this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Getter
@Setter
public class ErrorResponse {

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
    private Date timestamp;
    private int code;
    private String status;
    private Object message;
    private String path;
    private String stackTrace;


    public ErrorResponse() {
        timestamp = new Date();
    }

    public ErrorResponse(HttpStatus httpStatus, Object message) {
        this();

        this.code = httpStatus.value();
        this.status = httpStatus.name();
        this.message = message;
    }

    public ErrorResponse(HttpStatus httpStatus, Object message, String path) {
        this(httpStatus, message);
        this.path = path;
    }

    public ErrorResponse(HttpStatus httpStatus, Object message, String path, String stackTrace) {
        this(httpStatus, message, path);
        this.stackTrace = stackTrace;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above class I am using the getter and setter annotation from project lombok. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As the final step lets tell the spring boot that we have error handler controller defined and not to use the default one by specifying the below properties as below
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring.mvc.throw-exception-if-no-handler-found=true
spring.web.resources.add-mappings=false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please note this will work in spring boot version &amp;gt;=2.4 if version less 2.4 use below configuration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now lets run this app. I am testing for /about route in my app its handler is not defined.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmejy3fogdrd4xkjy8u39.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmejy3fogdrd4xkjy8u39.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for the reading. Let me know your thoughts in comment section.&lt;/p&gt;

</description>
      <category>java</category>
      <category>springboot</category>
      <category>handlewhitelabelerror</category>
    </item>
  </channel>
</rss>
