Bootstrap

从0开始搭建一个生产级SpringBoot2.0.X项目(三)SpringBoot接口统一返回和全局异常处理

前言

最近有个想法想整理一个内容比较完整springboot项目初始化Demo。

SpringBoot接口统一返回和全局异常处理,使用@ControllerAdvice+ @ExceptionHandler

 的组合来实现。

一、pom文件新增依赖

         <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

二、创建Json工具类、统一返回相关类

2.1JsonUtil 

package com.murg.bootdemo.util;

import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

/**
 * jackson的序列化和反序列化工具类,逐步替换gson,只留一个序列化框架
 */
@Component
public class JsonUtil {

	public static ObjectMapper objectMapper;

	public JsonUtil(ObjectMapper objectMapper) {
		JsonUtil.objectMapper = objectMapper;
	}

	public static ObjectMapper getObjectMapper() {
		return objectMapper;
	}

	/**
	 * 根据json字符串解析对象(不带解密)
	 */
	@SneakyThrows
	public static <T> T fromJson(String json, Class<T> c) {
		ObjectMapper gson = getObjectMapper();
		return gson.readValue(json, c);
	}

	/**
	 * 根据json字符串解析对象(不带解密,第二个参数是Type,适用于泛型类型的返回类型...)
	 */
	@SneakyThrows
	public static <T> T fromJson(String json, TypeReference<T> type) {
		ObjectMapper gson = getObjectMapper();
		return gson.readValue(json, type);
	}

	public static Map<String, Object> fromJsonToMap(String json) {
		return fromJson(json, new TypeReference<Map<String, Object>>() {
		});
	}

	public static Map<String, String> fromJsonToStrMap(String json) {
		return fromJson(json, new TypeReference<Map<String, String>>() {
		});
	}


	/**
	 * 根据对象解析成json字符串
	 *
	 */
	@SneakyThrows
	public static String toJson(Object obj) {
		return getObjectMapper().writeValueAsString(obj);
	}

	public static Map<String, Object> fromJsonToMap(InputStream inputStream) throws IOException {
		return fromJson(inputStream, new TypeReference<Map<String, Object>>() {
		});
	}

	private static <T> T fromJson(InputStream inputStream, TypeReference<T> tTypeReference) throws IOException {
		return objectMapper.readValue(inputStream, tTypeReference);
	}

	/**
	 * map中取list
	 * @param map
	 * @param clazz
	 * @param key
	 * @return
	 */
	public  static <T> List<T> mapToList(Map<String, Object> map, Class clazz, String key) {
		Object o=map.get(key);
		String json=JSONArray.toJSONString(o);
		return JSONArray.parseArray(json,clazz);
	}


	public static void write2Stream(ServletOutputStream outputStream, Object webResult) throws IOException {
		objectMapper.writeValue(outputStream, webResult);
	}

	/**
	 *
	 * @description: 实体类转Map(可追加字段)
	 * @author: Jeff
	 * @date: 2019年10月29日
	 * @param object
	 * @return
	 */
	public static Map<String, Object> entityToMap(Object object,Map<String,Object> paramMap) {
		for (Field field : object.getClass().getDeclaredFields()) {
			try {
				boolean flag = field.isAccessible();
				field.setAccessible(true);
				Object o = field.get(object);
				paramMap.put(field.getName(), o);
				field.setAccessible(flag);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return paramMap;
	}

}

2.2ErrorCode 

package com.murg.bootdemo.exception;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
 */
@Getter
@RequiredArgsConstructor
public enum ErrorCode {
    ERROR_CODE_400(400, "参数异常!"),
    ;
    private final int errorCode;
    private final String errorMsg;
}

 2.3WebResult

package com.murg.bootdemo.common;


import com.murg.bootdemo.exception.ErrorCode;
import com.murg.bootdemo.util.JsonUtil;
import lombok.Data;
import org.slf4j.MDC;
import java.util.*;

@Data
public class WebResult<T> {
    private T data;
    private int code = SysType.WEB_RESULT_OK;
    private int msgType = SysType.WEB_RESULT_MSG_ALERT;
    private String msg = "";

    private final String trace = Objects.toString(MDC.get("traceId"), "");

    public WebResult<T> setMsgType(int msgType) {
        this.msgType = msgType;
        return this;
    }

    public WebResult() {
    }

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

    public T getData() {
        return data;
    }

    public WebResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    public int getCode() {
        return code;
    }

    public WebResult<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public WebResult<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public WebResult<T> isOK() {
        this.code = SysType.WEB_RESULT_OK;
        return this;
    }

    public WebResult<T> isWrong() {
        this.code = SysType.WEB_RESULT_WRONG;
        this.msgType = SysType.WEB_RESULT_MSG_ERROR;
        return this;
    }

    public WebResult<T> isWrong(String msg) {
        this.code = SysType.WEB_RESULT_WRONG;
        this.msgType = SysType.WEB_RESULT_MSG_ERROR;
        this.msg = msg;
        return this;
    }


    public WebResult<T> setErrorCode(int errorCode) {
        this.code = errorCode;
        return this;
    }

    public static <T> WebResult<T> ok() {
        return new WebResult<>();
    }

    public static <T> WebResult<T> ok(T data) {
        return WebResult.<T>ok().setData(data);
    }

    public static <T> WebResult<T> wrong() {
        return new WebResult<>(SysType.WEB_RESULT_WRONG, "操作失败!");
    }

    public static <T> WebResult<T> init(int code, String msg) {
        return new WebResult<>(code, msg);
    }

    public static <T> WebResult<T> wrong(String msg) {
        return new WebResult<>(SysType.WEB_RESULT_WRONG, msg);
    }

    public WebResult<T> alert(String msg) {
        this.setMsgType(SysType.WEB_RESULT_MSG_ALERT);
        this.setMsg(msg);
        return this;
    }

    public WebResult<T> alert(String msg, String yes) {
        this.setMsgType(SysType.WEB_RESULT_MSG_ALERT);
        this.setMsg(msg);
        return this;
    }

    public static <T> WebResult<T> confirm(String msg) {
        return new WebResult<T>().setMsgType(SysType.WEB_RESULT_MSG_CONFIRM).setMsg(msg);
    }

    public static <T> WebResult<T> setErrorCode(ErrorCode errorCode) {
        return WebResult.<T>wrong().setErrorCode(errorCode.getErrorCode()).setMsg(errorCode.getErrorMsg());
    }

    public WebResult<T> question(String msg, String yes) {
        this.setMsgType(SysType.WEB_RESULT_MSG_QUESTION);
        this.setMsg(msg);
        return this;
    }

    public WebResult<T> put(String key, Object val) {
        if (this.data == null) {
            this.data = (T) new HashMap<String, Object>();
        }
        ((Map<String, Object>)this.data).put(key, val);
        return this;
    }

    public WebResult<T> putAll(Object object) {
        if (this.data == null) {
            this.data = (T) new HashMap<String, Object>();
        }
        if (object instanceof Map) {
            ((Map<String, Object>)this.data).putAll((Map)object);
        } else {
            Map<String, Object> paramMap = JsonUtil.fromJsonToMap(JsonUtil.toJson(object));
            ((Map<String, Object>)this.data).putAll(paramMap);
        }
        return this;
    }

    public  WebResult<T> setMainMessage(String msg) {
        return setMsg(msg);
    }

    public  WebResult<T> setMessageType(int msgType) {
        return setMsgType(msgType);
    }

    public WebResult<T> relaod(String mes) {
        return this.alert(mes,"$reloadPage");
    }


}

三、创建全局异常处理配置

3.1BusinessException

创建自定义异常类BusinessException继承RuntimeException

package com.murg.bootdemo.exception;


/**
 * 业务级异常
 *
 *
 */
@SuppressWarnings("serial")
public class BusinessException extends RuntimeException {
	private String errMsg;
	private int errCode = -1;

    public BusinessException(Throwable cause) {

    }

    public BusinessException() {

	}

	public BusinessException(final String message, final Throwable cause) {
		super(message, cause);
		this.errMsg = message;
	}

	public BusinessException(ErrorCode errorCode) {
		this.errCode = errorCode.getErrorCode();
		this.errMsg = errorCode.getErrorMsg();
	}

	public BusinessException(String errMsg) {
		this.errMsg = errMsg;
	}

	public BusinessException(int errCode) {
		this.errCode = errCode;
	}

	public BusinessException(String errMsg, int errCode) {
		this.errMsg = errMsg;
		this.errCode = errCode;
	}

    @Override
    public Throwable fillInStackTrace() {
        return this;
    }

    public String getErrMsg() {
		return errMsg;
	}

	public BusinessException setErrMsg(String errMsg) {
		this.errMsg = errMsg;
        return this;
	}

	public int getErrCode() {
		return errCode;
	}

	public BusinessException setErrCode(int errCode) {
		this.errCode = errCode;
        return this;
	}

	@Override
	public String getMessage() {
		return errMsg;
	}


}

3.2自定义ErrorCode

package com.murg.bootdemo.exception;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
 */
@Getter
@RequiredArgsConstructor
public enum ErrorCode {
    ERROR_CODE_400(400, "参数异常!"),
    ;
    private final int errorCode;
    private final String errorMsg;
}

3.3自定义ExceptionUtil,获取堆栈信息

package com.murg.bootdemo.util;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Description: 系统异常处理
 */
public class ExceptionUtil {

    /**
     * 获取完整的堆栈信息
     * @param throwable
     * @return
     */
    public static String getStackTrace(Throwable throwable) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        throwable.printStackTrace(pw);
        return sw.getBuffer().toString();
    }

    /**
     * 获取异常堆栈信息 - 指定长度
     * @param throwable
     * @param size
     * @return
     */
    public static String getStackTrace(Throwable throwable,int size) {
        String stac = getStackTrace(throwable);
        if (stac.length() > size) {
            stac = stac.substring(0, size);
        }
        return stac;
    }
}

3.4数据库创建表MySysException,用于以后将错误信息记录

package com.murg.bootdemo.exception.po;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 */
@Getter
@Setter
@TableName("MY_SYS_EXCEPTION")
@KeySequence("MY_SYS_EXCEPTION_SEQ")
public class MySysException implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "EXCEPTIONID", type = IdType.INPUT)
    private BigDecimal exceptionid;

    private String userid;

    private String functionid;

    private String requesturl;

    private String requestparams;

    private Date exceptiondate;

    private Long exceptiontype;

    private Long exceptioncode;

    private String exceptionmsg;

    private String exceptionstac;


}

 3.5最后通过@ControllerAdvice创建一个全局异常的配置类ExceptionConfig 

package com.murg.bootdemo.config;


import com.murg.bootdemo.common.WebResult;
import com.murg.bootdemo.exception.BusinessException;
import com.murg.bootdemo.exception.ErrorCode;
import com.murg.bootdemo.exception.po.MySysException;
import com.murg.bootdemo.util.ExceptionUtil;
import com.murg.bootdemo.util.JsonUtil;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
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.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 公共全局异常处理
 */
@ControllerAdvice
@ResponseBody
@Slf4j
@RequiredArgsConstructor
public class ExceptionConfig {

	private final String defaultMessage = "系统出错,请与系统管理员联系!";
	private final Long EXCEPTION_CONTROLLER = 1L;
	private final Long EXCEPTION_SERVICE = 2L;

	@ExceptionHandler(BusinessException.class)
	public WebResult<Object> businessExceptionHandler(BusinessException businessException) {
		return new WebResult<>().alert(StringUtils.defaultString(businessException.getMessage(), defaultMessage))
				.setErrorCode(businessException.getErrCode());
	}

	@ExceptionHandler(MethodArgumentNotValidException.class)
	public WebResult<Object> handler(MethodArgumentNotValidException e) {
		log.error(e.getMessage(), e);
		BindingResult bindingResult = e.getBindingResult();
		List<Map<String, String>> collect = bindingResult.getAllErrors().stream()
				.map(objectError -> {
					ConstraintViolation<?> unwrap = objectError.unwrap(ConstraintViolation.class);
					Map<String, String> map = new HashMap<>(3);
					map.put("property", unwrap.getPropertyPath().toString());
					map.put("message", objectError.getDefaultMessage());
					return map;
				}).collect(Collectors.toList());
		return WebResult.setErrorCode(ErrorCode.ERROR_CODE_400).setData(collect).alert("参数错误!");
	}

	//处理请求参数格式错误 @RequestParam上validate失败后抛出的异常是javax.validation.ConstraintViolationException
	@ExceptionHandler(ConstraintViolationException.class)
	public WebResult<Object> handler(ConstraintViolationException e) {
		log.error(e.getMessage(), e);
		List<Map<String, String>> collect = e.getConstraintViolations().stream()
				.map(constraintViolation -> {
					Map<String, String> map = new HashMap<>(3);
					map.put("property", constraintViolation.getPropertyPath().toString());
					map.put("message", constraintViolation.getMessage());
					return map;
				}).collect(Collectors.toList());
		return WebResult.setErrorCode(ErrorCode.ERROR_CODE_400).setData(collect).alert("参数错误!");
	}

	@ExceptionHandler(Exception.class)
	public WebResult<Object> exceptionHandler(Exception exception, HttpServletRequest request) {
		log.error("系统错误", exception);
		// 简单记录下错误日志(复制的his表结构以及逻辑,后续应该会做调整)
		errorLogRecord(request, exception);
		return WebResult.wrong().alert(StringUtils.defaultString(exception.getMessage(), defaultMessage));
	}
	@ExceptionHandler(ExpiredJwtException.class)
	public WebResult<Object> exceptionHandler(ExpiredJwtException exception, HttpServletRequest request) {
		log.error("token解析错误", exception);
		WebResult<Object> webResult = WebResult.ok();
		webResult.isWrong("您可能未登录或登录超时,请重新登陆!");
		// 简单记录下错误日志(复制的his表结构以及逻辑,后续应该会做调整)
		errorLogRecord(request, exception);
		return webResult;
	}


	@ExceptionHandler(NoHandlerFoundException.class)
	@ResponseStatus(value = HttpStatus.NOT_FOUND)
	public WebResult<Object> handleNotFoundError(NoHandlerFoundException ex) {
		return WebResult.wrong("接口不存在!");
	}

	private void errorLogRecord(HttpServletRequest request, Exception e) {
		try {
			MySysException sysException = new MySysException();
			//异常类型
			sysException.setExceptiontype(EXCEPTION_CONTROLLER);

			//请求地址
			String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
			sysException.setRequesturl(basePath);
			//请求参数
			Map<String, String[]> params = new HashMap<>(request.getParameterMap());
			String param = JsonUtil.toJson(params);
			if (param.length() > 1800) {
				param = param.substring(0, 1800);
			}
			sysException.setRequestparams(param);
			//时间
			sysException.setExceptiondate(new Date());
			//异常信息
			String msg = e.getMessage();
			if (StringUtils.length(msg) > 200) {
				msg = msg.substring(0, 200);
			}
			sysException.setExceptionmsg(msg);
			//堆栈信息
			sysException.setExceptionstac(ExceptionUtil.getStackTrace(e, 1000));
			//后续正式的话保存信息
			//MySysExceptionMapper.insert(sysException);
		} catch (Exception exception) {
			//不处理
			log.error("拦截异常保存失败:" + exception.getMessage());
		}
	}

}
package com.murg.bootdemo.util;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Description: 系统异常处理
 */
public class ExceptionUtil {

    /**
     * 获取完整的堆栈信息
     * @param throwable
     * @return
     */
    public static String getStackTrace(Throwable throwable) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        throwable.printStackTrace(pw);
        return sw.getBuffer().toString();
    }

    /**
     * 获取异常堆栈信息 - 指定长度
     * @param throwable
     * @param size
     * @return
     */
    public static String getStackTrace(Throwable throwable,int size) {
        String stac = getStackTrace(throwable);
        if (stac.length() > size) {
            stac = stac.substring(0, size);
        }
        return stac;
    }
}

四、改造getTt26接口返回统一返回值

package com.murg.bootdemo.business.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.murg.bootdemo.business.entity.Tt26;
import com.murg.bootdemo.business.service.Tt26Service;
import com.murg.bootdemo.common.WebResult;
import lombok.RequiredArgsConstructor;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
@RestController
public class TestRestController {
    private final RestTemplate restTemplate;
    private final Tt26Service tt26Service;


    @RequestMapping(value = "/getTt26", method = RequestMethod.GET)
    public WebResult getTt26(@RequestParam String code){
        //通过不同的id获取不同的name
        return WebResult.ok(tt26Service.getOne(Wrappers.<Tt26>lambdaQuery().eq(Tt26::getCode,code)));
    }

}

出参结果

五、创建测试Controller测试全局异常 

package com.murg.bootdemo.business.controller;

import com.murg.bootdemo.exception.BusinessException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestExceptionController {


    //Get接口主要是用来获取信息的接口
    @RequestMapping(value = "/testbusiexception", method = RequestMethod.GET)
    public String testbussexception(){
      throw new BusinessException("测试全局异常拦截");
    }

    //Get接口主要是用来获取信息的接口
    @RequestMapping(value = "/testexception", method = RequestMethod.GET)
    public String testexception() throws Exception {
        throw new Exception("测试全局异常拦截2");
    }

}

接口访问自主抛出异常的接口看返回结果

;