import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.http.HttpStatus;
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;
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ResponseBody
public JSONObject errorHandler(Exception ex) {
log.error("参数异常捕获", ex);
JSONObject json = new JSONObject();
json.put("error", ex);
return json;
}
}
SpringBoot全局异常捕获
于 2021-08-01 18:30:23 首次发布