ControllerAdvice通用异常处理

通用异常处理

在web层的方法中如果出现异常,SpringMVC会自动帮我们处理,并向前端返回500状态码以及错误信息。但是这样的错误信息是不合理的,我们应该自行处理异常,让用户看到一个相对友好的页面。

如何处理统一异常

我们在学习Spring的时候,了解过AOP的概念,利用AOP可以帮助我们处理全局异常。但是切面切点这些的配置比较繁琐,SpringMVC为我们提供了简单的异常处理的方法。

案例

项目代码:

package com.leyou.web;

import com.leyou.pojo.Goods;
import com.leyou.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/6 8:41 下午
 * @Description:
 */
@RestController("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @PostMapping
    public ResponseEntity<Goods> saveGoods(Goods goods){
        //校验价格是否为空
        if(goods.getPrice() == null){
            throw new RuntimeException("商品价格不能为空");
        }
        Goods goods1 = goodsService.saveGoods(goods);
        return ResponseEntity.status(HttpStatus.CREATED).body(goods1);
    }

}

异常处理:

我们要使用之前需要先导入SpringMVC的jar包

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
          	<version>x.x</version>
        </dependency>

创建异常处理类:

package com.leyou.common.advice;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/7 11:06 上午
 * @Description:
 */
@ControllerAdvice
public class CommonExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<String> exceptionHandler(RuntimeException e){
        return new ResponseEntity<String>(e.getMessage(), HttpStatus.BAD_REQUEST);
    }
		//还可以声明其他类型异常的处理。。。
}

要注意的是:异常处理类必须要被Spring扫描。

此时不同于传统的异常只能返回500、错误信息含糊不清、响应体体为空。我们的统一异常处理的返回内容会更加清晰。使用postman查询接口后,返回响应体:商品价格不能为空

优化

此时虽然能实现统一异常处理,但是状态码仍然存在硬编码问题(同一类异常返回的都是同一个状态码),下面我们就来优化一下上面的案例:

创建异常信息枚举

枚举中存放了若干个该类型的实例,并且枚举是无法实例化的,因为其构造默认是private修饰的,所以可以粗浅的理解为枚举就是存放果然当前类个实例的类。

  • 枚举的实例声明可以简写为构造加参数的形式PRICE_COUNT_BE_NULL(400,"价格不能为空"),多个枚举实例之间以逗号间隔,最后一个实例以分号结束。

  • 枚举的实例必须在属性之前声明。

package com.leyou.common.enums;


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/7 11:28 上午
 * @Description: 错误异常信息枚举
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum  ExceptionEnums {

    PRICE_COUNT_BE_NULL(400,"价格不能为空"),
    PRICE_NAME_BE_NULL(400,"价格名称能为空"),
    PRICE_ID_BE_NULL(400,"价格不能为空"),
    PAGE_NOT_FOUND(404,"页面未找到")
    ;
    private Integer code;
    private String msg;
}

自定义异常

由于java提供的异常类无法封装状态码信息,我们需要自定义一个异常,用于异常处理。

package com.leyou.common.exception;

import com.leyou.common.enums.ExceptionEnums;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/7 11:37 上午
 * @Description:
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class MyRuntimeExcpetion extends RuntimeException{

    private ExceptionEnums exceptionEnums;

}

使用自定义异常优化异常处理类

package com.leyou.common.advice;

import com.leyou.common.enums.ExceptionEnums;
import com.leyou.common.exception.MyRuntimeExcpetion;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/7 11:06 上午
 * @Description: 通用异常处理
 */
@ControllerAdvice
public class CommonExceptionHandler {

    @ExceptionHandler(MyRuntimeExcpetion.class)
    public ResponseEntity<String> exceptionHandler(MyRuntimeExcpetion ex){
        //获取枚举
        ExceptionEnums em = ex.getExceptionEnums();
        //返回异常信息
        return ResponseEntity.status(em.getCode()).body(em.getMsg());
    }

}

业务代码优化

package com.leyou.web;

import com.leyou.common.enums.ExceptionEnums;
import com.leyou.common.exception.MyRuntimeExcpetion;
import com.leyou.pojo.Goods;
import com.leyou.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/6 8:41 下午
 * @Description:
 */
@RestController("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @PostMapping
    public ResponseEntity<Goods> saveGoods(Goods goods){
        //校验价格是否为空
        if(goods.getPrice() == null){
            throw new MyRuntimeExcpetion(ExceptionEnums.PRICE_COUNT_BE_NULL);
        }
        Goods goods1 = goodsService.saveGoods(goods);
        return ResponseEntity.status(HttpStatus.CREATED).body(goods1);
    }

}

此时已经可以完成状态码和异常信息的自定义了,但是此时对异常的信息还是不满意,只能返回简单的字符串提示。我们继续优化!

封装异常信息类

package com.leyou.common.vo;

import com.leyou.common.enums.ExceptionEnums;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/7 11:58 上午
 * @Description: 异常信息类
 */
@Data
public class ExceptionResult {

    /**
     * 状态码
     */
    private Integer status;

    /**
     * 异常信息
     */
    private String message;

    /**
     * 时间戳
     */
    private Long timestamp;

    public ExceptionResult(ExceptionEnums em){
        this.status = em.getCode();
        this.message = em.getMsg();
        this.timestamp = System.currentTimeMillis();
    }

}

再次优化通用异常处理

package com.leyou.common.advice;

import com.leyou.common.enums.ExceptionEnums;
import com.leyou.common.exception.MyRuntimeExcpetion;
import com.leyou.common.vo.ExceptionResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/7 11:06 上午
 * @Description: 通用异常处理
 */
@ControllerAdvice
public class CommonExceptionHandler {

    @ExceptionHandler(MyRuntimeExcpetion.class)
    public ResponseEntity<ExceptionResult> exceptionHandler(MyRuntimeExcpetion ex){
        //获取枚举
        ExceptionEnums em = ex.getExceptionEnums();
        //返回异常信息
        return ResponseEntity.status(em.getCode()).body(new ExceptionResult(em));
    }

}

除外ControllerAdvice注解还有如下两个作用

全局数据绑定

全局数据绑定功能可以用来做一些初始化的数据操作,我们可以将一些公共的数据定义在添加了 @ControllerAdvice 注解的类中,这样,在每一个 Controller 的接口中,就都能够访问导致这些数据。

使用步骤,首先定义全局数据,如下:

@ControllerAdvice
public class MyGlobalExceptionHandler {
    @ModelAttribute(name = "md")
    public Map<String,Object> mydata() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("age", 99);
        map.put("gender", "男");
        return map;
    }
}

使用 @ModelAttribute 注解标记该方法的返回数据是一个全局数据,默认情况下,这个全局数据的 key 就是返回的变量名,value 就是方法返回值,当然开发者可以通过 @ModelAttribute 注解的 name 属性去重新指定 key。

定义完成后,在任何一个Controller 的接口中,都可以获取到这里定义的数据:

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello(Model model) {
        Map<String, Object> map = model.asMap();
        System.out.println(map);
        int i = 1 / 0;
        return "hello controller advice";
    }
}

全局数据预处理

考虑我有两个实体类,Book 和 Author,分别定义如下:

public class Book {
    private String name;
    private Long price;
    //getter/setter
}
public class Author {
    private String name;
    private Integer age;
    //getter/setter
}

此时,如果我定义一个数据添加接口,如下:

@PostMapping("/book")
public void addBook(Book book, Author author) {
    System.out.println(book);
    System.out.println(author);
}

这个时候,添加操作就会有问题,因为两个实体类都有一个 name 属性,从前端传递时 ,无法区分。此时,通过 @ControllerAdvice 的全局数据预处理可以解决这个问题

解决步骤如下:

1.给接口中的变量取别名

@PostMapping("/book")
public void addBook(@ModelAttribute("b") Book book, @ModelAttribute("a") Author author) {
    System.out.println(book);
    System.out.println(author);
}

2.进行请求数据预处理
在 @ControllerAdvice 标记的类中添加如下代码:

@InitBinder("b")
public void b(WebDataBinder binder) {
    binder.setFieldDefaultPrefix("b.");
}
@InitBinder("a")
public void a(WebDataBinder binder) {
    binder.setFieldDefaultPrefix("a.");
}

@InitBinder("b") 注解表示该方法用来处理和Book和相关的参数,在方法中,给参数添加一个 b 前缀,即请求参数要有b前缀.

3.发送请求

请求发送时,通过给不同对象的参数添加不同的前缀,可以实现参数的区分.

补充

1、AOP中抛出的自定义异常需要继承RuntimeException.否则无法被全局异常处理监测

2、拦截器中的异常也可以被全局异常处理,处理到

参考文章:https://www.cnblogs.com/lenve/p/10748453.html

原文地址:https://www.cnblogs.com/zhangruifeng/p/13260051.html