SpringBoot方式:
1. 问题描述
修改springboot默认的返回错误信息,以适应前台ajax处理
{
error: "Internal Server Error"
message: "com.netflix.client.ClientException: Load balancer does not have available server for client: jcloud-provider-oauth"
path: "/oauth/oauthclientdetails/add"
status: 500
timestamp: "2019-06-01T14:03:39.865+0000"
trace: "java.lang.RuntimeException: com.netflix.client.Cli...."
}
SpringBoot在发生异常时候,会自动跳转到/error进行错误管理,该/error默认对应的BasicErrorController在包org.springframework.boot.autoconfigure.web.servlet.error中.

第一部分为处理页面请求
第二部分为处理非页面请求
2. 统一处理json的原因是因为SpringBoot返回的json不符合业务的需求,所以我们要进行改造,在errorHtml和error都调用调用了getErrorAttributes方法,该方法就生成返回json的Map.
因此,我们重写该方法,在获取map之后,再进行我们自己的处理即可。
3.改造方法
继承BasicErrorController

配置configuration使其生效

有问题,可以参考
网友评论