Spring属性拦截器@InitBinder搭配@RequestBody使用

前言:

Spring属性拦截器@InitBinder搭配@RequestBody使用,导致dto内属性拦截不到的问题。

/**
 * springMvc的属性编辑器,注:@RequestBody内成员不生效
 */
@InitBinder
public void initBinder(WebDataBinder binder) throws Exception {
    binder.registerCustomEditor(Integer.class, new IntegerEditor());
    binder.registerCustomEditor(String.class, new StringEditor());

    //入参校验统一拦截验证
    String className = binder.getObjectName().substring(0, 1).toUpperCase() + binder.getObjectName().substring(1);
    Class classDto = null;
    ArrayList<String> dtoPathList = new ArrayList<>();
    dtoPathList.add("com.park.test.system.dto.");
    dtoPathList.add("com.park.test.ucenter.dto.");
    dtoPathList.add("com.park.test.task.dto.");
    dtoPathList.add("com.park.test.other.dto.");
    int i = 0;
    while (classDto == null) {
        try {
            String allClassName = dtoPathList.get(i) + className;
            classDto = Class.forName(allClassName);
        } catch (Exception e) {
        }
        i++;
        //防止死循环
        if (i > dtoPathList.size()) {
            break;
        }
    }

    Object entity = binder.getTarget();

    for (Field field : ClassUtil.getAllSuperFields(classDto)) {
        if (field.getGenericType().toString().equals("class java.lang.Integer")) {
            // 如果类型是Integer
            field.setAccessible(true);
            Field file = ClassUtil.getDeclaredSuperField(classDto, field.getName());
            file.setAccessible(true);
            Object value = file.get(entity);
            log.info("获取到值为{}", value);
            log.info("spring属性拦截器拦截到入参为Integer类型参数{}值为{}", field.getName(), value);
            if (!ObjectUtil.isNull(value) && Integer.valueOf(value.toString()) < 0) {
                throw new ServiceException(2, "参数" + field.getName() + "必须大于0");
            }
        } else if (field.getGenericType().toString().equals("class java.lang.String")) {
            // 如果类型是String
            field.setAccessible(true);
            Field file = classDto.getDeclaredField(field.getName());
            file.setAccessible(true);
            Object value = file.get(entity);
            log.info("获取到值为{}", value);
            log.info("spring属性拦截器拦截到入参为String类型参数{}值为{}", field.getName(), value);
            if (!ObjectUtil.isNull(value) && value.toString().length() >= 2 && value.toString().trim().length() <= 0) {
                throw new ServiceException(2, "参数" + field.getName() + "不能全为空格");
            }
        }
    }
}

private class IntegerEditor extends PropertyEditorSupport {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if (!StringUtils.isEmpty(text) && Integer.valueOf(text) < 0) {
            throw new ServiceException(2, "Integer参数必须大于0");
        }
        setValue(text);
        return;
    }
}

private class StringEditor extends PropertyEditorSupport {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        if (!StringUtils.isEmpty(text) && text.trim().length() <= 0) {
            throw new ServiceException(2, "String传入参数不能全为空格");
        }
        setValue(text.trim());
        return;
    }
}
经验分享 程序员 微信小程序 职场和发展