博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
全局异常处理之自定义错误处理controller通过继承BasicErrorController
阅读量:2079 次
发布时间:2019-04-29

本文共 2107 字,大约阅读时间需要 7 分钟。

跟杨春娟学SpringBoot笔记:全局异常处理之自定义错误处理controller通过继承BasicErrorController

完成:第一遍

1.如何通过继承BasicErrorController实现自定义错误处理controller?

步骤一:创建ErrorPageController类

ErrorPageController.java

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, List
errorViewResolvers) {
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

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/

你可能感兴趣的文章
Java 设计模式 轻读汇总版
查看>>
Paxos学习笔记及图解
查看>>
深入解析Spring使用枚举接收参数和返回值机制并提供自定义最佳实践
查看>>
数据序列化框架——Kryo
查看>>
布隆过滤器(BloomFilter)——应用(三)
查看>>
MPP架构数据库优化总结——华为LibrA(MPPDB、GuassDB)与GreenPlum
查看>>
Spark代码可读性与性能优化——示例七(构建聚合器,以用于复杂聚合)
查看>>
Spark代码可读性与性能优化——示例八(一个业务逻辑,多种解决方式)
查看>>
简单理解 HTTPS
查看>>
简单理解 NAT
查看>>
RPC框架——Thrift简单示例
查看>>
RPC框架——gRPC简单示例
查看>>
JVM对象头的简单记录
查看>>
从Java代码到Java堆——理解并优化你的应用的内存使用量
查看>>
Redis持久化与过期机制
查看>>
关于在网络中使用BIO、NIO、AIO的示例
查看>>
网络通信框架——Netty示例
查看>>
网络通信框架——KyroNet示例
查看>>
JVM对synchronized的优化——锁膨胀
查看>>
MySQL中的索引 B+Tree
查看>>