10
10
import org .springframework .beans .BeansException ;
11
11
import org .springframework .context .ApplicationContext ;
12
12
import org .springframework .context .ApplicationContextAware ;
13
+ import org .springframework .core .annotation .AnnotationUtils ;
13
14
import org .springframework .stereotype .Controller ;
14
15
import org .springframework .util .StringUtils ;
15
16
import org .springframework .web .bind .annotation .DeleteMapping ;
@@ -59,17 +60,19 @@ private void initData(Map<String, Object> beanMap) {
59
60
Class <?> clz = bean .getClass ();
60
61
Method [] methods = clz .getMethods ();
61
62
for (Method method : methods ) {
62
- if (method .isAnnotationPresent (Encrypt .class )) {
63
- // 注解中的URI优先级高
64
- String uri = method .getAnnotation (Encrypt .class ).value ();
63
+ Encrypt encrypt = AnnotationUtils .findAnnotation (method , Encrypt .class );
64
+ if (encrypt != null ) {
65
+ // 注解中的URI优先级高
66
+ String uri = encrypt .value ();
65
67
if (!StringUtils .hasText (uri )) {
66
68
uri = getApiUri (clz , method );
67
69
}
68
70
logger .debug ("Encrypt URI: {}" , uri );
69
71
responseEncryptUriList .add (uri );
70
- }
71
- if (method .isAnnotationPresent (Decrypt .class )) {
72
- String uri = method .getAnnotation (Decrypt .class ).value ();
72
+ }
73
+ Decrypt decrypt = AnnotationUtils .findAnnotation (method , Decrypt .class );
74
+ if (decrypt != null ) {
75
+ String uri = decrypt .value ();
73
76
if (!StringUtils .hasText (uri )) {
74
77
uri = getApiUri (clz , method );
75
78
}
@@ -85,42 +88,44 @@ private String getApiUri(Class<?> clz, Method method) {
85
88
String methodType = "" ;
86
89
StringBuilder uri = new StringBuilder ();
87
90
88
- if (clz .isAnnotationPresent (RequestMapping .class )) {
89
- uri .append (formatUri (clz .getAnnotation (RequestMapping .class ).value ()[0 ]));
91
+ RequestMapping reqMapping = AnnotationUtils .findAnnotation (clz , RequestMapping .class );
92
+ if (reqMapping != null ) {
93
+ uri .append (formatUri (reqMapping .value ()[0 ]));
90
94
}
91
95
92
- if (method .isAnnotationPresent (GetMapping .class )) {
93
-
96
+ GetMapping getMapping = AnnotationUtils .findAnnotation (method , GetMapping .class );
97
+ PostMapping postMapping = AnnotationUtils .findAnnotation (method , PostMapping .class );
98
+ RequestMapping requestMapping = AnnotationUtils .findAnnotation (method , RequestMapping .class );
99
+ PutMapping putMapping = AnnotationUtils .findAnnotation (method , PutMapping .class );
100
+ DeleteMapping deleteMapping = AnnotationUtils .findAnnotation (method , DeleteMapping .class );
101
+
102
+ if (getMapping != null ) {
94
103
methodType = HttpMethodTypePrefixConstant .GET ;
95
- uri .append (formatUri (method . getAnnotation ( GetMapping . class ) .value ()[0 ]));
104
+ uri .append (formatUri (getMapping .value ()[0 ]));
96
105
97
- } else if (method .isAnnotationPresent (PostMapping .class )) {
98
-
106
+ } else if (postMapping != null ) {
99
107
methodType = HttpMethodTypePrefixConstant .POST ;
100
- uri .append (formatUri (method .getAnnotation (PostMapping .class ).value ()[0 ]));
101
-
102
- } else if (method .isAnnotationPresent (RequestMapping .class )) {
103
-
104
- RequestMapping requestMapping = method .getAnnotation (RequestMapping .class );
105
- RequestMethod m = requestMapping .method ()[0 ];
106
- methodType = m .name ().toLowerCase () + ":" ;
107
- uri .append (formatUri (requestMapping .value ()[0 ]));
108
+ uri .append (formatUri (postMapping .value ()[0 ]));
108
109
109
- } else if (method .isAnnotationPresent (PutMapping .class )) {
110
-
110
+ } else if (putMapping != null ) {
111
111
methodType = HttpMethodTypePrefixConstant .PUT ;
112
- uri .append (formatUri (method . getAnnotation ( PutMapping . class ) .value ()[0 ]));
112
+ uri .append (formatUri (putMapping .value ()[0 ]));
113
113
114
- } else if (method .isAnnotationPresent (DeleteMapping .class )) {
115
-
114
+ } else if (deleteMapping != null ) {
116
115
methodType = HttpMethodTypePrefixConstant .DELETE ;
117
- uri .append (formatUri (method . getAnnotation ( DeleteMapping . class ) .value ()[0 ]));
116
+ uri .append (formatUri (deleteMapping .value ()[0 ]));
118
117
119
- }
118
+ } else if (requestMapping != null ) {
119
+ RequestMethod m = requestMapping .method ()[0 ];
120
+ methodType = m .name ().toLowerCase () + ":" ;
121
+ uri .append (formatUri (requestMapping .value ()[0 ]));
122
+
123
+ }
120
124
121
125
if (StringUtils .hasText (this .contextPath )) {
122
126
return methodType + this .contextPath + uri .toString ();
123
127
}
128
+
124
129
return methodType + uri .toString ();
125
130
}
126
131
0 commit comments