Spring Cloud Gateway配置自定义异常返回
0. 前言 最近搞微服务的全家桶,用到的Spring Cloud Gateway 这个组件。需要对这个网关抛出的异常进行自定义。网关的异常处理跟单体SpringBoot的全局异常处理还有点不一样。 单体全局异常处理,是采用@RestControllerAdvice 这个注解来实现的。微服务Gateway是采用另外的方式来实现的。 1. 单体自定义异常返回 CustomException.java 1 package com.wunaozai.config.advice; 2 3 /** 4 * 自定义异常类(运行时异常) 5 * @author wunaozai 6 * @date 2018-06-27 7 */ 8 public class CustomException extends RuntimeException { 9 10 private static final long serialVersionUID = 6304501072268270030L ; 11 12 public CustomException(String msg) { 13 this (500 , msg); 14 } 15 public CustomException( int code, String msg) { 16 this (code, msg, null ); 17