1
1
package me .chanjar .weixin .common .util ;
2
2
3
- import java .lang .reflect .Field ;
4
- import java .util .ArrayList ;
5
- import java .util .Arrays ;
6
- import java .util .List ;
7
- import java .util .Map ;
8
-
9
- import org .slf4j .Logger ;
10
- import org .slf4j .LoggerFactory ;
11
-
12
3
import com .google .common .collect .Lists ;
13
4
import com .google .common .collect .Maps ;
14
5
import com .thoughtworks .xstream .annotations .XStreamAlias ;
15
-
16
6
import me .chanjar .weixin .common .annotation .Required ;
17
7
import me .chanjar .weixin .common .bean .result .WxError ;
18
8
import me .chanjar .weixin .common .exception .WxErrorException ;
9
+ import org .apache .commons .lang3 .StringUtils ;
10
+ import org .slf4j .Logger ;
11
+ import org .slf4j .LoggerFactory ;
12
+
13
+ import java .lang .reflect .Field ;
14
+ import java .util .ArrayList ;
15
+ import java .util .Arrays ;
16
+ import java .util .List ;
17
+ import java .util .Map ;
19
18
20
19
/**
21
20
* <pre>
25
24
* </pre>
26
25
*/
27
26
public class BeanUtils {
28
- private static Logger log = LoggerFactory .getLogger (BeanUtils .class );
27
+ private static Logger log = LoggerFactory .getLogger (BeanUtils .class );
29
28
30
29
/**
31
30
* 检查bean里标记为@Required的field是否为空,为空则抛异常
31
+ *
32
32
* @param bean 要检查的bean对象
33
33
* @throws WxErrorException
34
34
*/
35
35
public static void checkRequiredFields (Object bean ) throws WxErrorException {
36
- List <String > nullFields = Lists .newArrayList ();
36
+ List <String > requiredFields = Lists .newArrayList ();
37
37
38
- List <Field > fields = new ArrayList <>( Arrays .asList (bean .getClass ().getDeclaredFields ()));
38
+ List <Field > fields = new ArrayList <>(Arrays .asList (bean .getClass ().getDeclaredFields ()));
39
39
fields .addAll (Arrays .asList (bean .getClass ().getSuperclass ().getDeclaredFields ()));
40
40
for (Field field : fields ) {
41
41
try {
42
42
boolean isAccessible = field .isAccessible ();
43
43
field .setAccessible (true );
44
- if (field .isAnnotationPresent (Required .class )
45
- && field .get (bean ) == null ) {
46
- nullFields .add (field .getName ());
44
+ if (field .isAnnotationPresent (Required .class )) {
45
+ if (field .get (bean ) == null || (field .get (bean ) instanceof String && StringUtils .isBlank (field .get (bean ).toString ()))) {
46
+ //两种情况,一种是值为null,另外一种情况是类型为字符串,但是字符串内容为空的,都认为是没有提供值
47
+ requiredFields .add (field .getName ());
48
+ }
47
49
}
48
50
field .setAccessible (isAccessible );
49
51
} catch (SecurityException | IllegalArgumentException
@@ -52,43 +54,43 @@ public static void checkRequiredFields(Object bean) throws WxErrorException {
52
54
}
53
55
}
54
56
55
- if (!nullFields .isEmpty ()) {
56
- String msg = "必填字段 " + nullFields + " 必须提供值" ;
57
+ if (!requiredFields .isEmpty ()) {
58
+ String msg = "必填字段 " + requiredFields + " 必须提供值" ;
57
59
log .debug (msg );
58
60
throw new WxErrorException (WxError .newBuilder ().setErrorMsg (msg ).build ());
59
61
}
60
62
}
61
63
62
- /**
63
- * 将bean按照@XStreamAlias标识的字符串内容生成以之为key的map对象
64
- *
65
- * @param bean 包含@XStreamAlias的xml bean对象
66
- * @return map对象
67
- */
68
- public static Map <String , String > xmlBean2Map (Object bean ) {
69
- Map <String , String > result = Maps .newHashMap ();
70
- List <Field > fields = new ArrayList <>(Arrays .asList (bean .getClass ().getDeclaredFields ()));
71
- fields .addAll (Arrays .asList (bean .getClass ().getSuperclass ().getDeclaredFields ()));
72
- for (Field field : fields ) {
73
- try {
74
- boolean isAccessible = field .isAccessible ();
75
- field .setAccessible (true );
76
- if (field .get (bean ) == null ) {
77
- field .setAccessible (isAccessible );
78
- continue ;
79
- }
64
+ /**
65
+ * 将bean按照@XStreamAlias标识的字符串内容生成以之为key的map对象
66
+ *
67
+ * @param bean 包含@XStreamAlias的xml bean对象
68
+ * @return map对象
69
+ */
70
+ public static Map <String , String > xmlBean2Map (Object bean ) {
71
+ Map <String , String > result = Maps .newHashMap ();
72
+ List <Field > fields = new ArrayList <>(Arrays .asList (bean .getClass ().getDeclaredFields ()));
73
+ fields .addAll (Arrays .asList (bean .getClass ().getSuperclass ().getDeclaredFields ()));
74
+ for (Field field : fields ) {
75
+ try {
76
+ boolean isAccessible = field .isAccessible ();
77
+ field .setAccessible (true );
78
+ if (field .get (bean ) == null ) {
79
+ field .setAccessible (isAccessible );
80
+ continue ;
81
+ }
80
82
81
- if (field .isAnnotationPresent (XStreamAlias .class )) {
82
- result .put (field .getAnnotation (XStreamAlias .class ).value (), field .get (bean ).toString ());
83
- }
83
+ if (field .isAnnotationPresent (XStreamAlias .class )) {
84
+ result .put (field .getAnnotation (XStreamAlias .class ).value (), field .get (bean ).toString ());
85
+ }
84
86
85
- field .setAccessible (isAccessible );
86
- } catch (SecurityException | IllegalArgumentException | IllegalAccessException e ) {
87
- e .printStackTrace ();
88
- }
87
+ field .setAccessible (isAccessible );
88
+ } catch (SecurityException | IllegalArgumentException | IllegalAccessException e ) {
89
+ e .printStackTrace ();
90
+ }
89
91
90
- }
92
+ }
91
93
92
- return result ;
93
- }
94
+ return result ;
95
+ }
94
96
}
0 commit comments