spring-boot全局异常

1.创建一个全局异常处理类

  • @ControllerAdvice 可以用来返回统一的错误json或者是统一错误jsp页面
  • @ExceptionHandler(value = Exception.class)中的异常必须继承Throwable
  • @ResponseBody Object 返回json格式字符串
  • ModelAndView 返回一个视图
package com.bjpowernode.springboot.handler;

import com.bjpowernode.springboot.constants.Constant;
import com.bjpowernode.springboot.model.ResultObject;
import com.bjpowernode.springboot.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;

/**
 * 也可以使用@RestControllerAdvice,它用于直接返回json的,不能返回jsp页面的
 */
@ControllerAdvice //可以用来返回统一的错误json或者是统一错误jsp页面
public class GlobalExceptionHandler {

    //也可以注入其他的bean
    @Autowired
    private UsersService usersService;

    /**
     * 方式一:统一返回一个错误的json格式的数据
     *
     * @param request
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = FileNotFoundException.class)
    public @ResponseBody Object errorHandlerByJson(HttpServletRequest request, Exception e) throws Exception {
        //可以拿到异常信息
        //e.printStackTrace();

        System.out.println(usersService);
        usersService.show();

        //可以返回统一数据
        return new ResultObject(Constant.ONE, "Sorry,服务器开小差啦~");
    }

    /**
     * 当发生异常的时候,跳转到统一的错误页
     *
     * @param request
     * @param e
     * @return
     * @throws Exception
     */
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = Exception.class)
    public ModelAndView errorHandlerByView(HttpServletRequest request, Exception e) throws Exception {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("exception", e.getMessage());
        modelAndView.addObject("url", request.getRequestURL());

        //设置发生异常的时候,跳转到哪个页面
        modelAndView.setViewName("50x");
        //可以返回统一数据
        return modelAndView;
    }
}

2.SpringBoot 404页面处理
2.1创建一个异常处理bean

package com.bjpowernode.springboot.handler;

import com.bjpowernode.springboot.constants.Constant;
import com.bjpowernode.springboot.model.ResultObject;
import com.bjpowernode.springboot.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;

/**
 * 也可以使用@RestControllerAdvice,它用于直接返回json的,不能返回jsp页面的
 */
@ControllerAdvice //可以用来返回统一的错误json或者是统一错误jsp页面
public class GlobalExceptionHandler {

    

    /**
     * 处理404页面找不到错误
     * 配置一个bean
     *
     */
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        return ( factory -> {
            ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404");
            factory.addErrorPages(error404Page);
        });
    }
}

2.2 创建一个异常处理类

package com.bjpowernode.springboot.handler;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 异常处理类
 */
@Controller
public class GlobalNotFundHandler {

    /**
     * 当发生404的时候,统一走这个地方
     *
     * @return
     */
    @RequestMapping("/404")
    public String notFund() {
        return "40x";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值