From aad521a7f83124db409c2f6f02423540079e3e46 Mon Sep 17 00:00:00 2001 From: cfl Date: Thu, 7 Sep 2023 08:05:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B0=91=E6=97=8F=E5=92=8C=E8=81=8C=E4=B8=9A?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=AD=97=E6=AE=B5=E7=B1=BB=E5=9E=8B=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../talkonlineweb/aop/GlobalExceptionHandler.java | 151 --------------------- .../aop/ValidatedExceptionHandler.java | 47 +++++++ .../lyms/talkonlineweb/domain/LymsXljcArchive.java | 2 +- .../service/impl/LymsXljcRecordServiceImpl.java | 2 +- 4 files changed, 49 insertions(+), 153 deletions(-) delete mode 100644 talkonlineweb/src/main/java/com/lyms/talkonlineweb/aop/GlobalExceptionHandler.java create mode 100644 talkonlineweb/src/main/java/com/lyms/talkonlineweb/aop/ValidatedExceptionHandler.java diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/aop/GlobalExceptionHandler.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/aop/GlobalExceptionHandler.java deleted file mode 100644 index 649cab7..0000000 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/aop/GlobalExceptionHandler.java +++ /dev/null @@ -1,151 +0,0 @@ -package com.lyms.talkonlineweb.aop; - -import com.lyms.talkonlineweb.constants.ErrorCodeConstants; -import com.lyms.talkonlineweb.result.BaseResponse; -import lombok.extern.log4j.Log4j2; -import org.springframework.aop.AopInvocationException; -import org.springframework.dao.DataAccessResourceFailureException; -import org.springframework.validation.BindException; -import org.springframework.web.bind.MissingServletRequestParameterException; -import org.springframework.web.bind.annotation.ControllerAdvice; - -import org.springframework.http.HttpStatus; -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.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.util.HashMap; -import java.util.List; -import java.util.Map; - -@Log4j2 -@ControllerAdvice -public class GlobalExceptionHandler { - - private String globalMsg = "服务器内部错误,请联系管理员"; - - - /** - * 处理@Validated参数校验失败异常 - * @param exception 异常类 - * @return 响应 - */ - @ResponseBody - @ResponseStatus(HttpStatus.BAD_REQUEST) - @ExceptionHandler(MethodArgumentNotValidException.class) - public BaseResponse exceptionHandler(MethodArgumentNotValidException exception){ - BindingResult result = exception.getBindingResult(); - StringBuilder stringBuilder = new StringBuilder(); - if (result.hasErrors()) { - List errors = result.getAllErrors(); - if (errors != null) { - errors.forEach(p -> { - FieldError fieldError = (FieldError) p; - log.warn("Bad Request Parameters: dto entity [{}],field [{}],message [{}]",fieldError.getObjectName(), fieldError.getField(), fieldError.getDefaultMessage()); - stringBuilder.append(fieldError.getDefaultMessage()+";"); - }); - } - } - BaseResponse baseResponse=new BaseResponse(); - baseResponse.setErrorcode(1); - baseResponse.setErrormsg(stringBuilder.toString()); - return baseResponse; - } - - @ExceptionHandler(org.springframework.dao.DataAccessResourceFailureException.class) - @ResponseBody - @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - public BaseResponse handException(DataAccessResourceFailureException e) { - log.error(e); - BaseResponse error = new BaseResponse(); - error.setErrorcode(ErrorCodeConstants.SYSTEM_ERROR); - error.setErrormsg(globalMsg); - return error; - } - - @ExceptionHandler(org.springframework.aop.AopInvocationException.class) - @ResponseBody - @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - public BaseResponse handException(AopInvocationException e) { - log.error(e); - BaseResponse error = new BaseResponse(); - error.setErrorcode(ErrorCodeConstants.SYSTEM_ERROR); - error.setErrormsg(globalMsg); - return error; - } - - - @ExceptionHandler(BindException.class) - @ResponseStatus(HttpStatus.OK) - @ResponseBody - public Map validationError(BindException e, HttpServletResponse httpServletResponse) { - setHttpResponseHeader(httpServletResponse); - - return createValidationResponse(e.getBindingResult()); - - } - - - /** - * 处理spring mvc 缺少请求参数的异常提示 - * - * @param httpServletResponse - * @param missRequestParameter - */ - @ResponseStatus(HttpStatus.OK) - @ExceptionHandler(org.springframework.web.bind.MissingServletRequestParameterException.class) - @ResponseBody - public Map handlMissingServletRequestParameter(HttpServletResponse httpServletResponse, - MissingServletRequestParameterException missRequestParameter, - HttpServletRequest httpServletRequest) { - setHttpResponseHeader(httpServletResponse); - Map resultMap = new HashMap<>(); - resultMap.put("errorcode", ErrorCodeConstants.PARAMETER_ERROR); - resultMap.put("errormsg", missRequestParameter.getMessage()); - log.error("ExceptionHandlerController handlMissingServletRequestParameter." + httpServletRequest.getRequestURI() + "?" + httpServletRequest.getQueryString(),missRequestParameter); - return resultMap; - } - - - @ResponseStatus(HttpStatus.OK) - @ExceptionHandler(Exception.class) - @ResponseBody - public Map buildException(HttpServletResponse httpServletResponse, Exception e, HttpServletRequest httpServletRequest) { - setHttpResponseHeader(httpServletResponse); - Map resultMap = new HashMap<>(); - resultMap.put("errorcode", ErrorCodeConstants.SYSTEM_ERROR); - resultMap.put("errormsg", "服务器异常."); - resultMap.put("message", e.getMessage()); - resultMap.put("exception", e.getClass()); - if(!e.getClass().getSimpleName().equals("ClientAbortException") ){ - log.error("ExceptionHandlerController Exception. queryStr: " +httpServletRequest.getRequestURI() + "?"+httpServletRequest.getQueryString(),e); - } - - return resultMap; - } - - private Map createValidationResponse(BindingResult bindingResult) { - Map map = new HashMap(); - List fieldErrors = bindingResult.getFieldErrors(); - for (org.springframework.validation.FieldError fieldError : fieldErrors) { - map.put("errorcode", ErrorCodeConstants.PARAMETER_ERROR); - map.put("errormsg","参数错误"); - break; - } - return map; - } - - private void setHttpResponseHeader(HttpServletResponse httpServletResponse) { - httpServletResponse.setCharacterEncoding("UTF-8"); - httpServletResponse.setHeader("Cache-Control", "no-cache"); - - httpServletResponse.setContentType("application/json;charset=UTF-8"); - } - -} diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/aop/ValidatedExceptionHandler.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/aop/ValidatedExceptionHandler.java new file mode 100644 index 0000000..20250f6 --- /dev/null +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/aop/ValidatedExceptionHandler.java @@ -0,0 +1,47 @@ +package com.lyms.talkonlineweb.aop; + +import com.lyms.talkonlineweb.result.BaseResponse; +import lombok.extern.log4j.Log4j2; +import org.springframework.web.bind.annotation.ControllerAdvice; + +import org.springframework.http.HttpStatus; +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.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import java.util.List; + +@Log4j2 +@ControllerAdvice +public class ValidatedExceptionHandler { + /** + * 处理@Validated参数校验失败异常 + * @param exception 异常类 + * @return 响应 + */ + @ResponseBody + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(MethodArgumentNotValidException.class) + public BaseResponse exceptionHandler(MethodArgumentNotValidException exception){ + BindingResult result = exception.getBindingResult(); + StringBuilder stringBuilder = new StringBuilder(); + if (result.hasErrors()) { + List errors = result.getAllErrors(); + if (errors != null) { + errors.forEach(p -> { + FieldError fieldError = (FieldError) p; + log.warn("Bad Request Parameters: dto entity [{}],field [{}],message [{}]",fieldError.getObjectName(), fieldError.getField(), fieldError.getDefaultMessage()); + stringBuilder.append(fieldError.getDefaultMessage()+";"); + }); + } + } + BaseResponse baseResponse=new BaseResponse(); + baseResponse.setErrorcode(1); + baseResponse.setErrormsg(stringBuilder.toString()); + return baseResponse; + } + +} diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsXljcArchive.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsXljcArchive.java index dda895e..058ae09 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsXljcArchive.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsXljcArchive.java @@ -77,7 +77,7 @@ public class LymsXljcArchive implements Serializable { * 民族 */ @TableField(value = "nation") - private String nation; + private Integer nation; /** * 职业类别 diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/impl/LymsXljcRecordServiceImpl.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/impl/LymsXljcRecordServiceImpl.java index 6f12f07..3bd68f4 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/impl/LymsXljcRecordServiceImpl.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/impl/LymsXljcRecordServiceImpl.java @@ -211,7 +211,7 @@ public class LymsXljcRecordServiceImpl extends ServiceImpl