Commit 17752986 by xuwenqian

feat:增加统一参数校验

parent 1b1d2b0a
......@@ -143,7 +143,7 @@ public class GoodsDomainController extends BaseGoodsController {
GoodsChannelVO goodsChannelVO = new GoodsChannelVO();
goodsChannelVO.setChannelCode(entry.getKey());
goodsChannelVO.setChannelName(Constant.CHANNEL_CODE_AND_NAME.get(entry.getKey()));
goodsChannelVO.setSceneCode(Constant.CHANNEL_CODE_AND_SCENE.get(entry.getKey()));
goodsChannelVO.setSceneCode(Constant.ES_CODES_MAP.get(entry.getKey()));
goodsChannelVO.setGoodsDomainList(new ArrayList<>());
for (GoodsDomainDTO goodsDomainDTO : entry.getValue()) {
GoodsDomainVO goodsDomainVO = new GoodsDomainVO();
......
package com.gic.plug.web.exception;
import com.gic.commons.exception.BaseRuntimeException;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.enterprise.exception.CommonException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* 统一参数校验
*
* @author xuwenqian
*/
@RestControllerAdvice
public class SpringGlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(SpringGlobalExceptionHandler.class);
@ExceptionHandler(value = Exception.class)
public RestResponse handlerException(Exception exception) {
String message = exception.getMessage();
return RestResponse.failure(com.gic.goods.api.dto.ErrorCode.UNKNOW_ERROR.getCode(), com.gic.goods.api.dto.ErrorCode.UNKNOW_ERROR.getErrorMessage());
}
@ExceptionHandler(value = NullPointerException.class)
public RestResponse nullExceptionHandler(HttpServletRequest req, NullPointerException e) {
log.error("发生空指针异常!原因是:", e);
return RestResponse.failure(com.gic.goods.api.dto.ErrorCode.UNKNOW_ERROR.getCode(), e.getMessage());
}
/**
* 统一参数校验
*
* @param ex
* @return
*/
@ExceptionHandler(BindException.class)
public RestResponse handleBindException(BindException ex) {
FieldError fieldError = ex.getBindingResult().getFieldError();
return RestResponse.failure(com.gic.enterprise.error.ErrorCode.MISS_PARAMETER.getCode(), fieldError.getDefaultMessage());
}
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public RestResponse handleNotValid(MethodArgumentNotValidException exception) {
String message = exception.getBindingResult().getFieldError().getDefaultMessage();
return RestResponse.failure(com.gic.goods.api.dto.ErrorCode.INVALIDATE_PARAM.getCode(), message);
}
@ExceptionHandler(value = ConstraintViolationException.class)
public RestResponse methodConstraintViolationException(ConstraintViolationException exception) {
List<String> msg = new ArrayList<>();
Set<ConstraintViolation<?>> constraintViolations = exception.getConstraintViolations();
RestResponse result = new RestResponse();
result.setCode(com.gic.goods.api.dto.ErrorCode.INVALIDATE_PARAM.getCode());
for (ConstraintViolation<?> constraintViolation : constraintViolations) {
msg.add(constraintViolation.getMessage());
}
result.setMessage(StringUtils.join(msg, ","));
return result;
}
@ExceptionHandler(value = CommonException.class)
public RestResponse enterpriseCommonException(CommonException exception) {
return RestResponse.failure(exception.getErrorCode(), exception.getMessage());
}
@ExceptionHandler(value = BaseRuntimeException.class)
public RestResponse baseRuntimeException(BaseRuntimeException exception) throws Exception {
return RestResponse.failure(exception.getErrCode(), exception.getMessage());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment