mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 18:46:10 +08:00 
			
		
		
		
	修复方法
This commit is contained in:
		| @ -1,22 +1,21 @@ | |||||||
| package me.chanjar.weixin.common.util; | package me.chanjar.weixin.common.util; | ||||||
|  |  | ||||||
|  | import com.google.common.collect.Lists; | ||||||
|  | import com.google.common.collect.Maps; | ||||||
|  | import com.thoughtworks.xstream.annotations.XStreamAlias; | ||||||
|  | import me.chanjar.weixin.common.annotation.Required; | ||||||
|  | import me.chanjar.weixin.common.bean.result.WxError; | ||||||
|  | import me.chanjar.weixin.common.exception.WxErrorException; | ||||||
|  | import org.apache.commons.lang3.StringUtils; | ||||||
|  | import org.slf4j.Logger; | ||||||
|  | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
| import java.lang.reflect.Field; | import java.lang.reflect.Field; | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.Arrays; | import java.util.Arrays; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Map; | import java.util.Map; | ||||||
|  |  | ||||||
| import org.slf4j.Logger; |  | ||||||
| import org.slf4j.LoggerFactory; |  | ||||||
|  |  | ||||||
| import com.google.common.collect.Lists; |  | ||||||
| import com.google.common.collect.Maps; |  | ||||||
| import com.thoughtworks.xstream.annotations.XStreamAlias; |  | ||||||
|  |  | ||||||
| import me.chanjar.weixin.common.annotation.Required; |  | ||||||
| import me.chanjar.weixin.common.bean.result.WxError; |  | ||||||
| import me.chanjar.weixin.common.exception.WxErrorException; |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * <pre> |  * <pre> | ||||||
|  * bean操作的一些工具类 |  * bean操作的一些工具类 | ||||||
| @ -25,25 +24,28 @@ import me.chanjar.weixin.common.exception.WxErrorException; | |||||||
|  * </pre> |  * </pre> | ||||||
|  */ |  */ | ||||||
| public class BeanUtils { | public class BeanUtils { | ||||||
| 	private static Logger log = LoggerFactory.getLogger(BeanUtils.class); |   private static Logger log = LoggerFactory.getLogger(BeanUtils.class); | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * 检查bean里标记为@Required的field是否为空,为空则抛异常 |    * 检查bean里标记为@Required的field是否为空,为空则抛异常 | ||||||
|  |    * | ||||||
|    * @param bean 要检查的bean对象 |    * @param bean 要检查的bean对象 | ||||||
|    * @throws WxErrorException |    * @throws WxErrorException | ||||||
|    */ |    */ | ||||||
|   public static void checkRequiredFields(Object bean) throws WxErrorException { |   public static void checkRequiredFields(Object bean) throws WxErrorException { | ||||||
|     List<String> nullFields = Lists.newArrayList(); |     List<String> requiredFields = Lists.newArrayList(); | ||||||
|  |  | ||||||
|     List<Field> fields = new ArrayList<>( Arrays.asList(bean.getClass().getDeclaredFields())); |     List<Field> fields = new ArrayList<>(Arrays.asList(bean.getClass().getDeclaredFields())); | ||||||
|     fields.addAll(Arrays.asList(bean.getClass().getSuperclass().getDeclaredFields())); |     fields.addAll(Arrays.asList(bean.getClass().getSuperclass().getDeclaredFields())); | ||||||
|     for (Field field : fields) { |     for (Field field : fields) { | ||||||
|       try { |       try { | ||||||
|         boolean isAccessible = field.isAccessible(); |         boolean isAccessible = field.isAccessible(); | ||||||
|         field.setAccessible(true); |         field.setAccessible(true); | ||||||
|         if (field.isAnnotationPresent(Required.class) |         if (field.isAnnotationPresent(Required.class)) { | ||||||
|           && field.get(bean) == null) { |           if (field.get(bean) == null || (field.get(bean) instanceof String && StringUtils.isBlank(field.get(bean).toString()))) { | ||||||
|           nullFields.add(field.getName()); |             //两种情况,一种是值为null,另外一种情况是类型为字符串,但是字符串内容为空的,都认为是没有提供值 | ||||||
|  |             requiredFields.add(field.getName()); | ||||||
|  |           } | ||||||
|         } |         } | ||||||
|         field.setAccessible(isAccessible); |         field.setAccessible(isAccessible); | ||||||
|       } catch (SecurityException | IllegalArgumentException |       } catch (SecurityException | IllegalArgumentException | ||||||
| @ -52,43 +54,43 @@ public class BeanUtils { | |||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (!nullFields.isEmpty()) { |     if (!requiredFields.isEmpty()) { | ||||||
|       String msg  = "必填字段 " + nullFields + " 必须提供值";	 |       String msg = "必填字段 " + requiredFields + " 必须提供值"; | ||||||
|       log.debug(msg); |       log.debug(msg); | ||||||
|       throw new WxErrorException(WxError.newBuilder().setErrorMsg(msg).build()); |       throw new WxErrorException(WxError.newBuilder().setErrorMsg(msg).build()); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
| 	/** |   /** | ||||||
| 	 * 将bean按照@XStreamAlias标识的字符串内容生成以之为key的map对象 |    * 将bean按照@XStreamAlias标识的字符串内容生成以之为key的map对象 | ||||||
| 	 *  |    * | ||||||
| 	 * @param bean 包含@XStreamAlias的xml bean对象 |    * @param bean 包含@XStreamAlias的xml bean对象 | ||||||
| 	 * @return map对象 |    * @return map对象 | ||||||
| 	 */ |    */ | ||||||
| 	public static Map<String, String> xmlBean2Map(Object bean) { |   public static Map<String, String> xmlBean2Map(Object bean) { | ||||||
| 		Map<String, String> result = Maps.newHashMap(); |     Map<String, String> result = Maps.newHashMap(); | ||||||
| 		List<Field> fields = new ArrayList<>(Arrays.asList(bean.getClass().getDeclaredFields())); |     List<Field> fields = new ArrayList<>(Arrays.asList(bean.getClass().getDeclaredFields())); | ||||||
| 		fields.addAll(Arrays.asList(bean.getClass().getSuperclass().getDeclaredFields())); |     fields.addAll(Arrays.asList(bean.getClass().getSuperclass().getDeclaredFields())); | ||||||
| 		for (Field field : fields) { |     for (Field field : fields) { | ||||||
| 			try { |       try { | ||||||
| 				boolean isAccessible = field.isAccessible(); |         boolean isAccessible = field.isAccessible(); | ||||||
| 				field.setAccessible(true); |         field.setAccessible(true); | ||||||
| 				if (field.get(bean) == null) { |         if (field.get(bean) == null) { | ||||||
| 					field.setAccessible(isAccessible); |           field.setAccessible(isAccessible); | ||||||
| 					continue; |           continue; | ||||||
| 				} |         } | ||||||
|  |  | ||||||
| 				if (field.isAnnotationPresent(XStreamAlias.class)) { |         if (field.isAnnotationPresent(XStreamAlias.class)) { | ||||||
| 					result.put(field.getAnnotation(XStreamAlias.class).value(), field.get(bean).toString()); |           result.put(field.getAnnotation(XStreamAlias.class).value(), field.get(bean).toString()); | ||||||
| 				} |         } | ||||||
|  |  | ||||||
| 				field.setAccessible(isAccessible); |         field.setAccessible(isAccessible); | ||||||
| 			} catch (SecurityException | IllegalArgumentException | IllegalAccessException e) { |       } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) { | ||||||
| 				e.printStackTrace(); |         e.printStackTrace(); | ||||||
| 			} |       } | ||||||
|  |  | ||||||
| 		} |     } | ||||||
|  |  | ||||||
| 		return result; |     return result; | ||||||
| 	} |   } | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang