第四章: Spring Boot 异常处理机制

自定义异常,在日常项目开发中用处很大

一:默认全局异常处理方法

1、先创建一个异常统一处理类 

package com.black.example.helloworld.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 异常统一处理方法
 */
@ControllerAdvice
public class MyHandlerAdvice {
    /**
     * 全局捕获异常,只要作用在@RequestMapping方法上,所有的信息都会被捕捉到
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Map<String,Object> errorHandler(Exception ex){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",-1);
        map.put("msg",ex.getMessage());
        return  map;
    }
}

2、假定在HelloWorldHandler中的index方法上,设置一些异常代码,如下图:

@RequestMapping(value = "/index.html")
    public String index(){
        int code = 1/0;
        return "Hello Black!!!!!";
    }

3、重启访问 http://localhost:8090/black/index.html

二:自定义异常

1、自定义异常实体类

package com.black.example.helloworld.exception;

/**
 * 自定义异常
 */
public class BusinessException extends RuntimeException {
    private String code;
    private String msg;

    public BusinessException(String code,String msg){
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

2、自定义异常方法

package com.black.example.helloworld.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 异常统一处理方法
 */
@ControllerAdvice
public class MyHandlerAdvice {
    /**
     * 全局捕获异常,只要作用在@RequestMapping方法上,所有的信息都会被捕捉到
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Map<String,Object> errorHandler(Exception ex){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",-1);
        map.put("msg",ex.getMessage());
        return  map;
    }

    /**
     * 自定义异常
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public Map<String,Object> errorBussinessHandler(BusinessException ex){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",ex.getCode());
        map.put("msg",ex.getMsg());
        return  map;
    }


}

3、假定程序抛出异常

package com.black.example.helloworld.web;

import com.black.example.helloworld.exception.BusinessException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by 10250H on 2018/5/15.
 */
@RestController
public class HelloWorldHandler {
    //获取application.properties自定义配置的值
    @Value("${black.msg}")
    private String msg;

    @RequestMapping(value = "/index.html")
    public String index(){
        /*int code = 1/0;
        return "Hello Black!!!!!";*/

        throw new BusinessException("500","用户名或者密码错误");
    }


    @RequestMapping(value = "/customConfig.html")
    public String customConfig(){
        return this.msg;
    }
}

4、重启服务,观察结果

猜你喜欢

转载自my.oschina.net/zupengliu/blog/1813739
今日推荐