编程语言
首页 > 编程语言> > java后台过滤vue前台传的空字符串

java后台过滤vue前台传的空字符串

作者:互联网

将""过滤变成null

	private static final String STRING_TYPE = "java.lang.String";


	public static <T> T objFilter(T object) {
		if (object == null) {
			return object;
		}
		// 获取所有属性
		Class clazz = object.getClass();
		Field[] fields = clazz.getDeclaredFields();

		for (Field field : fields) {
			//设置属性是可以访问的(包括私有)
			field.setAccessible(true);
			// 如果是空串,设置为null
			if (STRING_TYPE.equals(field.getGenericType().getTypeName())) {
				try {
					if (ValidateUtil.isEmpty(field.get(object))) {
						String name = field.getName();
						Class type = field.getType();
						Method method = clazz.getMethod("set" + name.replaceFirst(name.substring(0, 1),
								name.substring(0, 1).toUpperCase()), type);
						method.invoke(object, new Object[]{null});
					}
				} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
		return object;
	}

标签:vue,java,name,object,clazz,field,前台,null,String
来源: https://www.cnblogs.com/KJee26/p/16382148.html