spring自定义controller全局异常拦截

--异常类可以按需要自定义
package com.dhht.wechat.exception;


import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

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

/**
* @Author: sh
* @Description: GlobalExceptionHandler 异常
* @Date: 22:12 2019/6/13
*/
@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(value=Exception.class)
public Map<String,Object> exceptionHandler(Exception e){
e.printStackTrace();
Map<String,Object> resultMap = new HashMap<>();
resultMap.put("code",500);
resultMap.put("message","exception");
resultMap.put("data",null);
return resultMap;
}
}
原文地址:https://www.cnblogs.com/sung1024/p/11178396.html