1616import feign .DeclarativeContract ;
1717import feign .MethodMetadata ;
1818import feign .Request ;
19+ import feign .Util ;
20+ import java .lang .reflect .Parameter ;
1921import java .util .ArrayList ;
2022import java .util .Collection ;
2123import org .springframework .web .bind .annotation .*;
@@ -41,8 +43,7 @@ public SpringContract() {
4143 registerMethodAnnotation (
4244 RequestMapping .class ,
4345 (requestMapping , data ) -> {
44- String [] mappings = requestMapping .value ();
45- appendMappings (data , mappings );
46+ appendMappings (data , mapping (requestMapping .path (), requestMapping .value ()));
4647
4748 if (requestMapping .method ().length == 1 )
4849 data .template ().method (Request .HttpMethod .valueOf (requestMapping .method ()[0 ].name ()));
@@ -51,7 +52,7 @@ public SpringContract() {
5152 registerMethodAnnotation (
5253 GetMapping .class ,
5354 (mapping , data ) -> {
54- appendMappings (data , mapping . value ());
55+ appendMappings (data , mapping ( mapping . path (), mapping . value () ));
5556 data .template ().method (Request .HttpMethod .GET );
5657 handleProducesAnnotation (data , mapping .produces ());
5758 handleConsumesAnnotation (data , mapping .consumes ());
@@ -60,7 +61,7 @@ public SpringContract() {
6061 registerMethodAnnotation (
6162 PostMapping .class ,
6263 (mapping , data ) -> {
63- appendMappings (data , mapping . value ());
64+ appendMappings (data , mapping ( mapping . path (), mapping . value () ));
6465 data .template ().method (Request .HttpMethod .POST );
6566 handleProducesAnnotation (data , mapping .produces ());
6667 handleConsumesAnnotation (data , mapping .consumes ());
@@ -69,7 +70,7 @@ public SpringContract() {
6970 registerMethodAnnotation (
7071 PutMapping .class ,
7172 (mapping , data ) -> {
72- appendMappings (data , mapping . value ());
73+ appendMappings (data , mapping ( mapping . path (), mapping . value () ));
7374 data .template ().method (Request .HttpMethod .PUT );
7475 handleProducesAnnotation (data , mapping .produces ());
7576 handleConsumesAnnotation (data , mapping .consumes ());
@@ -78,7 +79,7 @@ public SpringContract() {
7879 registerMethodAnnotation (
7980 DeleteMapping .class ,
8081 (mapping , data ) -> {
81- appendMappings (data , mapping . value ());
82+ appendMappings (data , mapping ( mapping . path (), mapping . value () ));
8283 data .template ().method (Request .HttpMethod .DELETE );
8384 handleProducesAnnotation (data , mapping .produces ());
8485 handleConsumesAnnotation (data , mapping .consumes ());
@@ -87,7 +88,7 @@ public SpringContract() {
8788 registerMethodAnnotation (
8889 PatchMapping .class ,
8990 (mapping , data ) -> {
90- appendMappings (data , mapping . value ());
91+ appendMappings (data , mapping ( mapping . path (), mapping . value () ));
9192 data .template ().method (Request .HttpMethod .PATCH );
9293 handleProducesAnnotation (data , mapping .produces ());
9394 handleConsumesAnnotation (data , mapping .consumes ());
@@ -103,26 +104,54 @@ public SpringContract() {
103104 (ann , data ) -> {
104105 data .ignoreMethod ();
105106 });
106- registerParameterAnnotation (
107- PathVariable .class ,
108- (parameterAnnotation , data , paramIndex ) -> {
109- String name = PathVariable .class .cast (parameterAnnotation ).value ();
110- nameParam (data , name , paramIndex );
111- });
107+ registerParameterAnnotation (PathVariable .class , pathVariableParameterAnnotationProcessor ());
112108
113109 registerParameterAnnotation (
114110 RequestBody .class ,
115111 (body , data , paramIndex ) -> {
116112 handleProducesAnnotation (data , "application/json" );
117113 });
118- registerParameterAnnotation (
119- RequestParam .class ,
120- (parameterAnnotation , data , paramIndex ) -> {
121- String name = RequestParam .class .cast (parameterAnnotation ).value ();
122- Collection <String > query = addTemplatedParam (data .template ().queries ().get (name ), name );
123- data .template ().query (name , query );
124- nameParam (data , name , paramIndex );
125- });
114+ registerParameterAnnotation (RequestParam .class , requestParamParameterAnnotationProcessor ());
115+ }
116+
117+ private String [] mapping (String [] firstPriority , String [] fallback ) {
118+ return firstPriority .length > 0 ? firstPriority : fallback ;
119+ }
120+
121+ private String parameterName (String firstPriority , String secondPriority , Parameter parameter ) {
122+ if (Util .isNotBlank (firstPriority )) {
123+ return firstPriority ;
124+ } else if (Util .isNotBlank (secondPriority )) {
125+ return secondPriority ;
126+ } else {
127+ if (parameter .isNamePresent ()) {
128+ return parameter .getName ();
129+ } else {
130+ return firstPriority ;
131+ }
132+ }
133+ }
134+
135+ private DeclarativeContract .ParameterAnnotationProcessor <PathVariable >
136+ pathVariableParameterAnnotationProcessor () {
137+ return (parameterAnnotation , data , paramIndex ) -> {
138+ Parameter parameter = data .method ().getParameters ()[paramIndex ];
139+ String name =
140+ parameterName (parameterAnnotation .name (), parameterAnnotation .value (), parameter );
141+ nameParam (data , name , paramIndex );
142+ };
143+ }
144+
145+ private DeclarativeContract .ParameterAnnotationProcessor <RequestParam >
146+ requestParamParameterAnnotationProcessor () {
147+ return (parameterAnnotation , data , paramIndex ) -> {
148+ Parameter parameter = data .method ().getParameters ()[paramIndex ];
149+ String name =
150+ parameterName (parameterAnnotation .name (), parameterAnnotation .value (), parameter );
151+ Collection <String > query = addTemplatedParam (data .template ().queries ().get (name ), name );
152+ data .template ().query (name , query );
153+ nameParam (data , name , paramIndex );
154+ };
126155 }
127156
128157 private void appendMappings (MethodMetadata data , String [] mappings ) {
0 commit comments