|
|
@ -1,10 +1,14 @@ |
|
|
|
package com.ccsens.util.config; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.ccsens.util.CodeEnum; |
|
|
|
import com.ccsens.util.HttpServletUtil; |
|
|
|
import com.ccsens.util.JacksonUtil; |
|
|
|
import com.ccsens.util.JsonResponse; |
|
|
|
import com.ccsens.util.exception.BaseException; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
|
|
|
import org.springframework.http.HttpStatus; |
|
|
|
import org.springframework.http.converter.HttpMessageNotReadableException; |
|
|
|
import org.springframework.validation.BindException; |
|
|
|
import org.springframework.validation.BindingResult; |
|
|
@ -13,8 +17,11 @@ 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 javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.PrintWriter; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@ControllerAdvice |
|
|
@ -67,13 +74,21 @@ public class ControllerExceptionHandler { |
|
|
|
} |
|
|
|
|
|
|
|
@ExceptionHandler(value = BaseException.class) |
|
|
|
@ResponseBody |
|
|
|
public JsonResponse jsonBaseExceptionHandler(HttpServletRequest req, BaseException e) { |
|
|
|
// @ResponseBody
|
|
|
|
public void jsonBaseExceptionHandler(HttpServletRequest req, HttpServletResponse response, BaseException e) { |
|
|
|
log.info("BaseException,code:{},message:{}", e.getCode(), e.getMessage()); |
|
|
|
JsonResponse ok; |
|
|
|
if (e.getCodeEnum() != null) { |
|
|
|
return JsonResponse.newInstance().ok(e.getCodeEnum()); |
|
|
|
ok = JsonResponse.newInstance().ok(e.getCodeEnum()); |
|
|
|
} else { |
|
|
|
ok = JsonResponse.newInstance().fail(e.getCode(),e.getMessage()); |
|
|
|
} |
|
|
|
try { |
|
|
|
HttpServletUtil.responseJson(response, |
|
|
|
JacksonUtil.beanToJson(ok)); |
|
|
|
} catch (Exception e1) { |
|
|
|
log.error("返回异常出错:", e); |
|
|
|
} |
|
|
|
return JsonResponse.newInstance().fail(e.getCode(),e.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
|
@ExceptionHandler(value = Exception.class) |
|
|
|