@@ -11,13 +11,11 @@ Include the dependency to your project's pom.xml file:
11
11
12
12
``` xml
13
13
<dependencies >
14
- ...
15
- <dependency >
16
- <groupId >io.github.openfeign.form</groupId >
17
- <artifactId >feign-form</artifactId >
18
- <version >3.5.0</version >
19
- </dependency >
20
- ...
14
+ <dependency >
15
+ <groupId >io.github.openfeign.form</groupId >
16
+ <artifactId >feign-form</artifactId >
17
+ <version >3.5.0</version >
18
+ </dependency >
21
19
</dependencies >
22
20
```
23
21
@@ -27,31 +25,30 @@ Add `FormEncoder` to your `Feign.Builder` like so:
27
25
28
26
``` java
29
27
SomeApi github = Feign . builder()
30
- .encoder(new FormEncoder ())
31
- .target(SomeApi . class, " http://api.some.org" );
28
+ .encoder(new FormEncoder ())
29
+ .target(SomeApi . class, " http://api.some.org" );
32
30
```
33
31
34
32
Moreover, you can decorate the existing encoder, for example JsonEncoder like this:
35
33
36
34
``` java
37
35
SomeApi github = Feign . builder()
38
- .encoder(new FormEncoder (new JacksonEncoder ()))
39
- .target(SomeApi . class, " http://api.some.org" );
36
+ .encoder(new FormEncoder (new JacksonEncoder ()))
37
+ .target(SomeApi . class, " http://api.some.org" );
40
38
```
41
39
42
40
And use them together:
43
41
44
42
``` java
45
43
interface SomeApi {
46
44
47
- @RequestLine (" POST /json" )
48
- @Headers (" Content-Type: application/json" )
49
- void json (Dto dto );
50
-
51
- @RequestLine (" POST /form" )
52
- @Headers (" Content-Type: application/x-www-form-urlencoded" )
53
- void from (@Param (" field1" ) String field1 , @Param (" field2" ) String field2 );
45
+ @RequestLine (" POST /json" )
46
+ @Headers (" Content-Type: application/json" )
47
+ void json (Dto dto );
54
48
49
+ @RequestLine (" POST /form" )
50
+ @Headers (" Content-Type: application/x-www-form-urlencoded" )
51
+ void from (@Param (" field1" ) String field1 , @Param (" field2" ) String field2 );
55
52
}
56
53
```
57
54
@@ -62,14 +59,21 @@ You can specify two types of encoding forms by `Content-Type` header.
62
59
``` java
63
60
interface SomeApi {
64
61
65
- ...
62
+ @RequestLine (" POST /authorization" )
63
+ @Headers (" Content-Type: application/x-www-form-urlencoded" )
64
+ void authorization (@Param (" email" ) String email , @Param (" password" ) String password );
65
+
66
+ // Group all parameters within a POJO
67
+ @RequestLine (" POST /user" )
68
+ @Headers (" Content-Type: application/x-www-form-urlencoded" )
69
+ void addUser (User user );
66
70
67
- @RequestLine (" POST /authorization" )
68
- @Headers (" Content-Type: application/x-www-form-urlencoded" )
69
- void authorization (@Param (" email" ) String email , @Param (" password" ) String password );
71
+ class User {
70
72
71
- ...
73
+ Integer id;
72
74
75
+ String name;
76
+ }
73
77
}
74
78
```
75
79
@@ -78,38 +82,47 @@ interface SomeApi {
78
82
``` java
79
83
interface SomeApi {
80
84
81
- ...
85
+ // File parameter
86
+ @RequestLine (" POST /send_photo" )
87
+ @Headers (" Content-Type: multipart/form-data" )
88
+ void sendPhoto (@Param (" is_public" ) Boolean isPublic , @Param (" photo" ) File photo );
89
+
90
+ // byte[] parameter
91
+ @RequestLine (" POST /send_photo" )
92
+ @Headers (" Content-Type: multipart/form-data" )
93
+ void sendPhoto (@Param (" is_public" ) Boolean isPublic , @Param (" photo" ) byte [] photo );
82
94
83
- // File parameter
84
- @RequestLine (" POST /send_photo" )
85
- @Headers (" Content-Type: multipart/form-data" )
86
- void sendPhoto (@Param (" is_public" ) Boolean isPublic , @Param (" photo" ) File photo );
95
+ // FormData parameter
96
+ @RequestLine (" POST /send_photo" )
97
+ @Headers (" Content-Type: multipart/form-data" )
98
+ void sendPhoto (@Param (" is_public" ) Boolean isPublic , @Param (" photo" ) FormData photo );
87
99
88
- // byte[] parameter
89
- @RequestLine (" POST /send_photo" )
90
- @Headers (" Content-Type: multipart/form-data" )
91
- void sendPhoto (@Param ( " is_public " ) Boolean isPublic , @Param ( " photo " ) byte [] photo );
100
+ // Group all parameters within a POJO
101
+ @RequestLine (" POST /send_photo" )
102
+ @Headers (" Content-Type: multipart/form-data" )
103
+ void sendPhoto (MyPojo pojo );
92
104
93
- // FormData parameter
94
- @RequestLine (" POST /send_photo" )
95
- @Headers (" Content-Type: multipart/form-data" )
96
- void sendPhoto (@Param (" is_public" ) Boolean isPublic , @Param (" photo" ) FormData photo );
97
- ...
105
+ class MyPojo {
98
106
107
+ Boolean isPublic;
108
+
109
+ File photo;
110
+ }
99
111
}
100
112
```
101
113
102
114
In the example above, the ` sendPhoto ` method uses the ` photo ` parameter using three different supported types.
103
115
104
- * ` File ` will use the File's extension to detect the ` Content-Type ` .
105
- * ` byte[] ` will use ` application/octet-stream ` as ` Content-Type ` .
106
- * ` FormData ` will use the ` FormData ` 's ` Content-Type ` and ` fileName ` .
116
+ * ` File ` will use the File's extension to detect the ` Content-Type ` ;
117
+ * ` byte[] ` will use ` application/octet-stream ` as ` Content-Type ` ;
118
+ * ` FormData ` will use the ` FormData ` 's ` Content-Type ` and ` fileName ` ;
119
+ * Client's custom POJO for grouping parameters (including types above).
107
120
108
121
` FormData ` is custom object that wraps a ` byte[] ` and defines a ` Content-Type ` and ` fileName ` like this:
109
122
110
123
``` java
111
- FormData formData = new FormData (" image/png" , " filename.png" , myDataAsByteArray);
112
- someApi. sendPhoto(true , formData);
124
+ FormData formData = new FormData (" image/png" , " filename.png" , myDataAsByteArray);
125
+ someApi. sendPhoto(true , formData);
113
126
```
114
127
115
128
### Spring MultipartFile and Spring Cloud Netflix @FeignClient support
@@ -120,51 +133,55 @@ Include the dependencies to your project's pom.xml file:
120
133
121
134
``` xml
122
135
<dependencies >
123
- ...
124
- <dependency >
125
- <groupId >io.github.openfeign.form</groupId >
126
- <artifactId >feign-form</artifactId >
127
- <version >3.5.0</version >
128
- </dependency >
129
- <dependency >
130
- <groupId >io.github.openfeign.form</groupId >
131
- <artifactId >feign-form-spring</artifactId >
132
- <version >3.5.0</version >
133
- </dependency >
134
- ...
136
+ <dependency >
137
+ <groupId >io.github.openfeign.form</groupId >
138
+ <artifactId >feign-form</artifactId >
139
+ <version >3.5.0</version >
140
+ </dependency >
141
+ <dependency >
142
+ <groupId >io.github.openfeign.form</groupId >
143
+ <artifactId >feign-form-spring</artifactId >
144
+ <version >3.5.0</version >
145
+ </dependency >
135
146
</dependencies >
136
147
```
137
148
138
149
``` java
139
- @FeignClient (name = " file-upload-service" , configuration = FileUploadServiceClient . MultipartSupportConfig . class)
150
+ @FeignClient (
151
+ name = " file-upload-service" ,
152
+ configuration = FileUploadServiceClient . MultipartSupportConfig . class
153
+ )
140
154
public interface FileUploadServiceClient extends IFileUploadServiceClient {
141
155
142
- public class MultipartSupportConfig {
156
+ public class MultipartSupportConfig {
143
157
144
- @Autowired
145
- private ObjectFactory<HttpMessageConverters > messageConverters;
158
+ @Autowired
159
+ private ObjectFactory<HttpMessageConverters > messageConverters;
146
160
147
- @Bean
148
- public Encoder feignFormEncoder () {
149
- return new SpringFormEncoder (new SpringEncoder (messageConverters));
150
- }
161
+ @Bean
162
+ public Encoder feignFormEncoder () {
163
+ return new SpringFormEncoder (new SpringEncoder (messageConverters));
151
164
}
165
+ }
152
166
}
153
167
```
154
168
155
169
Or, if you don't need Spring's standard encoder:
156
170
157
171
``` java
158
- @FeignClient (name = " file-upload-service" , configuration = FileUploadServiceClient . MultipartSupportConfig . class)
172
+ @FeignClient (
173
+ name = " file-upload-service" ,
174
+ configuration = FileUploadServiceClient . MultipartSupportConfig . class
175
+ )
159
176
public interface FileUploadServiceClient extends IFileUploadServiceClient {
160
177
161
- public class MultipartSupportConfig {
178
+ public class MultipartSupportConfig {
162
179
163
- @Bean
164
- public Encoder feignFormEncoder () {
165
- return new SpringFormEncoder ();
166
- }
180
+ @Bean
181
+ public Encoder feignFormEncoder () {
182
+ return new SpringFormEncoder ();
167
183
}
184
+ }
168
185
}
169
186
```
170
187
@@ -174,38 +191,41 @@ To use this feature, include SpringManyMultipartFilesReader in the list of messa
174
191
175
192
``` java
176
193
@FeignClient (
177
- name = " ${feign.name}" ,
178
- url = " ${feign.url}"
179
- configuration = DownloadClient . ClientConfiguration . class)
194
+ name = " ${feign.name}" ,
195
+ url = " ${feign.url}"
196
+ configuration = DownloadClient . ClientConfiguration . class
197
+ )
180
198
public interface DownloadClient {
181
199
182
- @RequestMapping (
183
- value = " /multipart/download/{fileId}" ,
184
- method = GET )
185
- MultipartFile [] download (@PathVariable (" fileId" ) String fileId );
200
+ @RequestMapping (" /multipart/download/{fileId}" )
201
+ MultipartFile [] download (@PathVariable (" fileId" ) String fileId );
202
+
203
+ class ClientConfiguration {
204
+
205
+ @Autowired
206
+ private ObjectFactory<HttpMessageConverters > messageConverters;
207
+
208
+ @Bean
209
+ public Decoder feignDecoder () {
210
+ List<HttpMessageConverter<?> > springConverters =
211
+ messageConverters. getObject(). getConverters();
186
212
187
- class ClientConfiguration {
213
+ List<HttpMessageConverter<?> > decoderConverters =
214
+ new ArrayList<HttpMessageConverter<?> > (springConverters. size() + 1 ;
188
215
189
- @Autowired
190
- private ObjectFactory< HttpMessageConverters > messageConverters ;
216
+ decoderConverters . addAll(springConverters);
217
+ decoderConverters . add( new SpringManyMultipartFilesReader ( 4096 )) ;
191
218
192
- @Bean
193
- public Decoder feignDecoder () {
194
- final List<HttpMessageConverter<?> > springConverters = messageConverters. getObject(). getConverters();
195
- final List<HttpMessageConverter<?> > decoderConverters
196
- = new ArrayList<HttpMessageConverter<?> > (springConverters. size() + 1 );
219
+ HttpMessageConverters httpMessageConverters = new HttpMessageConverters (decoderConverters);
197
220
198
- decoderConverters. addAll(springConverters);
199
- decoderConverters. add(new SpringManyMultipartFilesReader (4096 ));
200
- final HttpMessageConverters httpMessageConverters = new HttpMessageConverters (decoderConverters);
221
+ return new SpringDecoder (new ObjectFactory<HttpMessageConverters > () {
201
222
202
- return new SpringDecoder (new ObjectFactory<HttpMessageConverters > () {
203
- @Override
204
- public HttpMessageConverters getObject () {
205
- return httpMessageConverters;
206
- }
207
- });
223
+ @Override
224
+ public HttpMessageConverters getObject () {
225
+ return httpMessageConverters;
208
226
}
227
+ });
209
228
}
229
+ }
210
230
}
211
231
```
0 commit comments