2

I'm using spring-boot 2.1.3 and I want to add an error page as a fallback. I tried adding an error.html under the templates folder but it didn't work.

Also tried to add ErrorController and classify a page for each error type and it didn't work either.

Are there any other configurations that I should make?

Thanks in advance.

Olian04
  • 6,480
  • 2
  • 27
  • 54
motawfik
  • 23
  • 1
  • 11

3 Answers3

1

The only thing I can think of (without any code sample provided) is that /error might not have permission to view. Please try to set the permission in SecurityConfigurations to allow /error to be accessed by all.

http.authorizeRequests().antMatchers("/error").permitAll();
Vinay Limbare
  • 151
  • 2
  • 16
  • Thanks for the response, I have added error to the antMatchers and still no change, as well as server.error.whitelabel.enabled=false in application.properties – motawfik Feb 26 '19 at 21:12
1

In addition to turning off the Whitelabel error page, which you can do by adding this property to your properties file:

server.error.whitelabel.enabled=false

Make sure you have your static error page (i.e. 404.html) in the following folder:

src/
 +- main/
     +- java/
     |   + <source code>
     +- resources/
         +- public/
             +- error/
                 +- 404.html

Source: Spring Boot docs

blurfus
  • 13,485
  • 8
  • 55
  • 61
  • Thanks for the response I have structured the files as you asked and still no change – motawfik Feb 26 '19 at 21:13
  • @motawfik can you elaborate? what exactly do you see on your browser? did you add the property to your properties file? – blurfus Feb 26 '19 at 21:14
  • { timestamp: 1551216779662, status: 404, error: "Not Found", message: "No message available", path: "/ss" } This is returned as JSON And yes I've added the property – motawfik Feb 26 '19 at 21:33
  • @motawfik you will have to edit your question and show us your controller/ or how you are handling the views... it seems you are not using default values!? – blurfus Feb 26 '19 at 21:35
  • right not I'm using no Controller for the errors, I'm trying to use the default error.html page for a fallback – motawfik Feb 26 '19 at 21:39
  • I am afraid that unless you provide more information on your question, I am unable to help you figure out what the issue is. The question contains little details. The defaults (as laid out on the docs) should work (indeed, they worked for me). If they do not work it's likely that your particular settings are modifying the default behaviour. Without extra information, I cannot do much – blurfus Feb 26 '19 at 21:44
  • Do you have anything mapped to `/ss` ? (any other resource or endpoint? - GET,POST, etc?) – blurfus Feb 26 '19 at 21:46
  • No there's no mapping for /ss (this same response is returned for any URL) What kind of more information that you need ? – motawfik Feb 26 '19 at 21:57
  • where is your application running (which context)? how do you know things are working? – blurfus Feb 26 '19 at 21:58
  • thanks for sharing, but when I added this line to my prperties file, the whitelabel appears again. I had two cases, If I delete it, I will be redirected to the same path, and if it is not, the whitelabel page appears. – BackToReal Nov 14 '19 at 17:01
  • If you find an answer useful, feel free to upvote. If you have a separate issue, ask a question and link it in the comments so we can have a look at it – blurfus Nov 14 '19 at 17:06
  • I was using "templates" instead "public" and it was NOT working. As soon as I changed to "public", it worked !!!!!!! Probably, it changed from Spring 1.5 to spring 2.1, because I have a project using "templates" in spring 1.5 and it is working ok. – Goose Mar 28 '20 at 17:09
  • @Goose possibly it changed. It's been a while since I last worked on 1.5 :) – blurfus Mar 28 '20 at 17:15
0

Directly adding the error.html in resources/templates also did not work for me. So I created a customized error controller and added the error.html in resources/static folder.

Source: https://www.baeldung.com/spring-boot-custom-error-page

@Controller
public class CustomizedErrorController implements ErrorController  {

    @RequestMapping("/error")
    public String handleError() {
        return "error.html";
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}

enter image description here

PRTJ
  • 980
  • 1
  • 8
  • 15