Java Spring Boot 删除 Web 应用程序的白标错误页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37769173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot Remove Whitelabel Error Page for web application
提问by Manu
I have a spring boot web application,which I am deploying as a war file in tomcat. I do not want the user to be displyed the white-label error page. I have progressed a bit on this, but need to re-direct the same to error page.
我有一个 spring boot web 应用程序,我将它部署为 tomcat 中的一个 war 文件。我不希望用户显示白标错误页面。我在这方面取得了一些进展,但需要将其重新定向到错误页面。
The below code is /error white-label error page is custom error message. But i want it to be re-directed to a error.jsp or error.html available under the template folder in resources of my web application. I tried changing the @RestControllerto @Controllerwith no luck.
下面的代码是 /error 白标错误页面是自定义错误消息。但我希望它被重定向到我的 web 应用程序资源中模板文件夹下可用的 error.jsp 或 error.html。我尝试将@RestController更改为@Controller,但没有运气。
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomErrorController implements ErrorController {
private static final String PATH = "/error";
@RequestMapping(value = PATH)
public String error() {
return "Unexpected error has happened.Please contact administrator!!!";
}
@Override
public String getErrorPath() {
System.out.println("-- Error Page GET --");
return "error";
}
}
Dependency
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>4.0.1.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>spring-aop</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- Provided (for embedded war support) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-core</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4</version>
</dependency>
回答by Jakub Pomyka?a
Turn off whitelabel error pages in application.properties
关闭白标错误页面 application.properties
server.error.whitelabel.enabled=false
server.error.whitelabel.enabled=false
回答by Saravana
You can write your own exception handler in Spring and redirect to the error page
您可以在 Spring 中编写自己的异常处理程序并重定向到错误页面
Below one is to handler the Exception
and its sub classes
下面一个是处理Exception
和它的子类
@ExceptionHandler(Exception.class)
public ModelAndView globalExceptionHandler(Exception e) {
ModelAndView modelAndView = new ModelAndView("error");
modelAndView.addObject("message", e.getMessage());
return modelAndView;
}
You can write any number of exception handlers in controller, also narrow the exception you need in handlers, return appropriate error page.
您可以在控制器中编写任意数量的异常处理程序,还可以缩小处理程序中所需的异常范围,返回适当的错误页面。
Have a look at the documentation
看看文档