|
@@ -0,0 +1,101 @@
|
|
|
+package com.zhongshu.reward.server.core.hanlder;
|
|
|
+
|
|
|
+import com.zhongshu.reward.client.ret.ResultContent;
|
|
|
+import com.zhongshu.reward.client.ret.ResultException;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.lang.Nullable;
|
|
|
+import org.springframework.validation.BindException;
|
|
|
+import org.springframework.validation.BindingResult;
|
|
|
+import org.springframework.validation.FieldError;
|
|
|
+import org.springframework.validation.ObjectError;
|
|
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
+import org.springframework.web.bind.MissingServletRequestParameterException;
|
|
|
+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.bind.annotation.RestControllerAdvice;
|
|
|
+import org.springframework.web.servlet.ModelAndView;
|
|
|
+import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
|
|
|
+import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@RestControllerAdvice
|
|
|
+@ResponseBody
|
|
|
+public class RestExceptionHandler extends DefaultHandlerExceptionResolver {
|
|
|
+
|
|
|
+ @ResponseStatus(HttpStatus.OK)
|
|
|
+ @ExceptionHandler({MethodArgumentNotValidException.class})
|
|
|
+ public ResultContent<String> paramExceptionHandler(MethodArgumentNotValidException exception) {
|
|
|
+ String msg = exception.getMessage();
|
|
|
+ BindingResult exceptions = exception.getBindingResult();
|
|
|
+// 判断异常中是否有错误信息,如果存在就使用异常中的消息,否则使用默认消息
|
|
|
+ if (exceptions.hasErrors()) {
|
|
|
+ List errors = exceptions.getAllErrors();
|
|
|
+ if (!errors.isEmpty()) {
|
|
|
+// 这里列出了全部错误参数,按正常逻辑,只需要第一条错误即可
|
|
|
+ FieldError fieldError = (FieldError) errors.get(0);
|
|
|
+ return ResultContent.buildFail(fieldError.getDefaultMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ResultContent.buildFail(msg);
|
|
|
+ }
|
|
|
+ @ExceptionHandler(Exception.class)
|
|
|
+ @ResponseStatus(HttpStatus.OK)
|
|
|
+ public ResultContent<String> exception(Exception exception) {
|
|
|
+ String msg = exception.getMessage();
|
|
|
+ log.error("绑定异常信息 ex={}", exception.getMessage(), exception);
|
|
|
+ return ResultContent.buildFail(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ExceptionHandler(BindException.class)
|
|
|
+ @ResponseStatus(HttpStatus.OK)
|
|
|
+ public ResultContent<String> bindException(BindException exception) {
|
|
|
+ String msg = exception.getMessage();
|
|
|
+ try {
|
|
|
+ if (exception.getBindingResult() != null) {
|
|
|
+ List<ObjectError> list = exception.getBindingResult().getAllErrors();
|
|
|
+ if (ObjectUtils.isNotEmpty(list) && list.size() > 0) {
|
|
|
+ ObjectError objectError = list.get(0);
|
|
|
+ if (objectError instanceof FieldError) {
|
|
|
+ FieldError fieldError = (FieldError) objectError;
|
|
|
+ msg = String.format("【%s】 %s", fieldError.getField(), fieldError.getDefaultMessage());
|
|
|
+ } else {
|
|
|
+ msg = String.format("%s", objectError.getDefaultMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ log.error("绑定异常信息 ex1={}", exception.getMessage(), exception);
|
|
|
+ return ResultContent.buildFail(msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected ModelAndView handleMissingServletRequestParameter(MissingServletRequestParameterException ex,
|
|
|
+ HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {
|
|
|
+
|
|
|
+ log.error("missing param exception,uri:{},url:{}", request.getRequestURI(), request.getRequestURL());
|
|
|
+ return super.handleMissingServletRequestParameter(ex, request, response, handler);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("exception : {}", e);
|
|
|
+ ModelAndView mv = new ModelAndView();
|
|
|
+ mv.addObject("state", "Fail");
|
|
|
+ mv.setView(new MappingJackson2JsonView());
|
|
|
+ mv.addObject("exception", ResultException.build(e));
|
|
|
+ mv.addObject("msg", e.getMessage());
|
|
|
+ return mv;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|