package com.son.exception;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.alibaba.fastjson.JSONObject;
@RestControllerAdvice
public class GlobalErrorController {
Logger logger = LoggerFactory.getLogger(GlobalErrorController.class);
@ExceptionHandler(TestException.class)
public JSONObject constraintDeclarationException(TestException e) {
logger.error("error", e);
JSONObject result = new JSONObject();
result.put("code", 0);
result.put("msg", e.getMessage());
return result;
}
}
创建对应的异常处理类
package com.son.exception;
public class TestException extends RuntimeException {
public TestException() {
super();
}
public TestException(String messag