本文共 2107 字,大约阅读时间需要 7 分钟。
1.如何通过继承BasicErrorController实现自定义错误处理controller?
步骤一:创建ErrorPageController类
package com.springboot.demo.SpringBootDemoProject.error;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.springframework.boot.autoconfigure.web.ErrorProperties;import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;import org.springframework.boot.web.servlet.error.ErrorAttributes;public class ErrorPageController extends BasicErrorController { public ErrorPageController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, ListerrorViewResolvers) { super(errorAttributes, errorProperties, errorViewResolvers); } @Override protected Map getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) { Map errorMap = super.getErrorAttributes(request, includeStackTrace); errorMap.remove("message"); errorMap.remove("path"); return errorMap; } }
2.如何通过配置注解来替换BasicErrorController的功能?
步骤一:创建配置ErrorConfiguration.java
package com.springboot.demo.SpringBootDemoProject.error;import java.util.List;import org.springframework.beans.factory.ObjectProvider;import org.springframework.boot.autoconfigure.web.ServerProperties;import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;import org.springframework.boot.web.servlet.error.ErrorAttributes;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class ErrorConfiguration { @Bean public ErrorPageController basicErrorController(ErrorAttributes errorAttributes, ServerProperties serverProperties, ObjectProvider
> errorViewResolversProvider) { return new ErrorPageController(errorAttributes, serverProperties.getError(), errorViewResolversProvider.getIfAvailable()); }}
转载地址:http://lrzqf.baihongyu.com/