sndu
sndu

Reputation: 1033

Spring : Default html error page is not properly displayed?

I want to redirect my web site to a default error page when there is a error like 404 , 406 and etc. I have followed an example and wrote following methods in the controller.

@Controller
public class HomeController {
private static final String PATH = "/error-page";

        public String getErrorPath() {
            return PATH;
        }

        @RequestMapping(method=RequestMethod.GET, value=PATH, produces="text/plain")
        @ResponseBody
        public Map<String, Object> handleGet(HttpServletRequest request) {

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("status", request.getAttribute("javax.servlet.error.status_code"));
            map.put("reason", request.getAttribute("javax.servlet.error.message"));

            return map;
        }

        @RequestMapping(method=RequestMethod.POST, value=PATH, produces="text/plain")
        @ResponseBody
        public Map<String, Object> handlePost(HttpServletRequest request) {

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("status", request.getAttribute("javax.servlet.error.status_code"));
            map.put("reason", request.getAttribute("javax.servlet.error.message"));

            return map;
        }
}

There is a html page as error-page.html in the above mentioned path. Although I got no errors, I cant display this error page while getting an error. Instead usual error message is shown. What is the issue here?

Any help is highly appreciated.

Upvotes: 0

Views: 1290

Answers (2)

I don't know if with "...usual error message is shown" you mean the White-label error page. But if you want to deactivate this and show your own error-page you have to put in your application.properties or yml:

server.error.whitelabel.enabled=false

And then add an ErrorController like this:

@Controller
public class MyErrorController implements ErrorController {

  private static final String PATH = "/error";

  public MyErrorController() {}

  @RequestMapping(value = PATH)
  public ModelAndView handleAllException() {
    ModelAndView modelAndView = new ModelAndView("error_template");
    return modelAndView;
  }

  @Override
  public String getErrorPath () {
    System.out.println("-- Error Page GET --");
    return "error";
  }

}

Upvotes: 1

Vipul Singh
Vipul Singh

Reputation: 393

you have to redirect to other controller
for example return "redirect:/controllerPath".
After that in that particular controller you should write the code to load the require jsp page.

@RequestMapping("/controllerPath")
public String loadErrorPage(){
 return "errorPage";
}

In your Case I dont see any method for catching error how ever you have this method

 public String getErrorPath() {
            return PATH;
        }  

if this method is getting called at some point after error do it like this

 public String getErrorPath() {
            return "redirect:"+PATH;
        }

hope this helps.

Upvotes: 1

Related Questions