Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fold feign forms into main project #2557

Merged
merged 105 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
56f5969
Initial commit
xxlabaza May 1, 2016
8e52800
Corrected deploy bug
xxlabaza May 1, 2016
d3b69e4
Added dependency installation to README.md
xxlabaza May 1, 2016
6ab1b4c
Add support for @FeignClient annotated interfaces.
tjuchniewicz Aug 22, 2016
ac2d66b
Merge pull request #1 from ebjwc/spring-cloud-netflix-1274
xxlabaza Sep 6, 2016
5ade5dd
- Added Tomasz Juchniewicz as a coauthor of the project;
xxlabaza Sep 6, 2016
fa5a56c
Corrected README.md.
xxlabaza Sep 6, 2016
5f9ec9e
- Added Java 6+ support and removed java.nio.file.Path support;
xxlabaza Sep 12, 2016
6f876c4
- Moved from custom group id to OpenFeign's;
xxlabaza Sep 15, 2016
dbf8921
Corrected pom's URLs
xxlabaza Sep 15, 2016
1a61c80
Corrected readme and pom
xxlabaza Sep 15, 2016
3f18920
Extract support for MultipartFile to separate module.
tjuchniewicz Sep 16, 2016
f921edf
Update README.md
tjuchniewicz Sep 16, 2016
409933b
Update README.md
tjuchniewicz Sep 16, 2016
f7b0616
Fix groupId in pom.xml.
tjuchniewicz Sep 18, 2016
670dcc1
Update README.md
tjuchniewicz Sep 18, 2016
207a9d6
Update README.md
tjuchniewicz Sep 18, 2016
00ec29b
Merge pull request #7 from OpenFeign/gh-3
xxlabaza Sep 18, 2016
3b99835
- Added javadoc for public and protected methods;
xxlabaza Sep 18, 2016
88039a4
- Corrected pom-files for deploying on bintray;
xxlabaza Sep 18, 2016
0fe8ac9
Added travis support
xxlabaza Sep 19, 2016
89c9571
Added skip tests in travis
xxlabaza Sep 19, 2016
3a7afc2
Fix for https://github.com/OpenFeign/feign-form/issues/12
Nov 3, 2016
622afb7
Merge pull request #13 from zhurpavel/issue_12_fix
xxlabaza Nov 8, 2016
ce80cba
Updated dependencies versions and readme
xxlabaza Nov 8, 2016
53805ef
README update
xxlabaza Nov 8, 2016
f4723d1
Build fix release
xxlabaza Nov 8, 2016
09dcd02
Added default content-type.
xxlabaza Nov 17, 2016
c5b2a83
Modified FormEncoder to ignore the case of the content type header
Feb 16, 2017
8118a71
Fix typo
Feb 17, 2017
badf46f
Merge pull request #14 from thorntonv/IgnoreContentTypeCase
xxlabaza Feb 19, 2017
9f8f873
Merge pull request #15 from seanjreilly/master
xxlabaza Feb 19, 2017
9ea050c
Version correct
Feb 20, 2017
154ccc1
Readme update
Feb 20, 2017
ce2257f
Dependencies and copyrights update
Feb 20, 2017
73d3ca4
Extract metadata from MultipartFile
tjuchniewicz Feb 23, 2017
c4e8466
- Replace tabs to spaces;
xxlabaza Feb 23, 2017
5f55216
Update to new version
xxlabaza Feb 23, 2017
6699720
Merge pull request #16 from OpenFeign/gh-11
xxlabaza Feb 23, 2017
c0af651
OSGified feign-form jars
Jun 6, 2017
c6fca17
Merge pull request #20 from bhathiya/master
xxlabaza Jun 8, 2017
a88613b
Update dependencies and increment project version
Jun 8, 2017
261b985
Lombok is now provided. Update dependencies
xxlabaza Aug 5, 2017
6ee0876
Refactoring
Nov 24, 2017
e62bc6d
Refactoring
Nov 27, 2017
1b3ac63
Update version
Nov 27, 2017
dfed8c6
Issue #18: Sets charset to null in RequestTemplate body for compatibi…
Nov 30, 2017
6e84ad1
Fix maven-jar-plugin version
Nov 30, 2017
9ac39cf
multipart/form-data reader
Jiguro Jan 8, 2018
2276855
multipart/form-data reader unit & integration test
Jiguro Jan 9, 2018
876dcd1
multipart/form-data reader javadoc update
Jiguro Jan 9, 2018
b48cf01
Add checkstyle, PMD and FindBugs
Jan 16, 2018
60a5a03
Merge pull request #30 from tf-haotri-pham/feature/feign_multipart_do…
xxlabaza Jan 16, 2018
6fa12be
Merge branch 'master' of github.com:xxlabaza/feign-form
Jan 16, 2018
d75b5b8
Add tf-haotri-pham featur for files response
Jan 16, 2018
da2c1a6
Checkstyle fix and few minor style fixes
Jan 16, 2018
ea35bd6
Animal sniffer fixes
Jan 16, 2018
94422bf
ContentType fallback to application/octet-stream when guess failed
zerda Jan 19, 2018
4827e72
Checkstyle fix
zerda Jan 19, 2018
b83e54b
Travis usage refactoring
xxlabaza Jan 19, 2018
461ebac
Merge pull request #33 from zerda/master
xxlabaza Jan 19, 2018
22dafeb
Correct Travis file
xxlabaza Jan 23, 2018
3d7292e
Merge branch 'master' of github.com:xxlabaza/feign-form
xxlabaza Jan 23, 2018
e21ed9e
Fix unsecure Travis pull-request builds
xxlabaza Mar 24, 2018
fcfe0a5
Remove unused block from Travis build
xxlabaza Mar 24, 2018
9c545e3
Added FormData object (#38)
GuiSim Mar 28, 2018
1c7bcac
Add FormData holder
Mar 28, 2018
c82e87b
Refactoring poms
Oct 3, 2018
37893d3
Add a file name field to form data
Oct 3, 2018
6d0a45a
Correct README.md
Oct 3, 2018
b0c4bac
Issue48 (#50)
xxlabaza Oct 29, 2018
66e1b6f
Fix issue #54, update deps (#55)
xxlabaza Dec 23, 2018
72ed933
Skip filename in Content-Disposition if it's null. (#53)
buckett Dec 23, 2018
c5af9ed
Add user's pojo writer
xxlabaza Dec 23, 2018
cc4a95f
Problems fix
xxlabaza Dec 23, 2018
be97067
Add Pojo writer
Dec 24, 2018
f668913
Update deps
Dec 24, 2018
8561d4f
Fix checkstyle
Dec 24, 2018
54e4370
Some fixes
Dec 24, 2018
53e9f4d
Fix readme
Dec 24, 2018
e1caf57
实例代码少个右括号 (#57)
gududexunli Jan 7, 2019
81a6145
Issue60 (#62)
xxlabaza Mar 3, 2019
86d788a
fix version
xxlabaza Mar 3, 2019
8a92f78
add a requirements readme section
xxlabaza Mar 3, 2019
3e6ffc2
some articles
xxlabaza Mar 3, 2019
a6197a1
Fixes #65 derive custom form key for urlencoded dto (#66)
marembo2008 Mar 29, 2019
1c47c2d
insignificant style changes
Mar 29, 2019
47e9f40
Fix MultipartFile support
Mar 29, 2019
1b3f2cf
fix checkstyle
Mar 29, 2019
3bf02f6
update version
Mar 29, 2019
6eebea7
update readme
Mar 29, 2019
6cf7435
fix readme
Mar 29, 2019
98e51d1
fix readme
Apr 18, 2019
c8f07a6
update copyright
Jun 24, 2024
875e66a
set new version
Jun 24, 2024
ef5fb55
bump deps
Jun 25, 2024
1558f0b
remove garbage
Jun 25, 2024
d00c80f
fix depricated API
Jun 25, 2024
3c3951d
remove useless comment
Jun 25, 2024
1577990
refactoring
Jun 25, 2024
63ba1e9
63 delegation leads to unexpected result (#122)
xxlabaza Jun 25, 2024
68092b6
Merge remote-tracking branch 'feign-forms/master' into forms
velo Sep 20, 2024
291f033
Format sources and fix build
velo Sep 21, 2024
93f2561
Update rewrite recipes
velo Sep 21, 2024
12c56e0
Remove needless file
velo Sep 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
258 changes: 258 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1269,3 +1269,261 @@ The Bill Of Material is a special POM file that groups dependency versions that
</dependencyManagement>
</project>
```
# Form Encoder

[![build_status](https://travis-ci.org/OpenFeign/feign-form.svg?branch=master)](https://travis-ci.org/OpenFeign/feign-form)
[![maven_central](https://maven-badges.herokuapp.com/maven-central/io.github.openfeign.form/feign-form/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.github.openfeign.form/feign-form)
[![License](http://img.shields.io/:license-apache-brightgreen.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)

This module adds support for encoding **application/x-www-form-urlencoded** and **multipart/form-data** forms.

## Add dependency

Include the dependency to your app:

**Maven**:

```xml
<dependencies>
...
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>4.0.0</version>
</dependency>
...
</dependencies>
```

**Gradle**:

```groovy
compile 'io.github.openfeign.form:feign-form:4.0.0'
```

## Requirements

The `feign-form` extension depend on `OpenFeign` and its *concrete* versions:

- all `feign-form` releases before **3.5.0** works with `OpenFeign` **9.\*** versions;
- starting from `feign-form`'s version **3.5.0**, the module works with `OpenFeign` **10.1.0** versions and greater.

> **IMPORTANT:** there is no backward compatibility and no any gurantee that the `feign-form`'s versions after **3.5.0** work with `OpenFeign` before **10.\***. `OpenFeign` was refactored in 10th release, so the best approach - use the freshest `OpenFeign` and `feign-form` versions.

Notes:

- [spring-cloud-openfeign](https://github.com/spring-cloud/spring-cloud-openfeign) uses `OpenFeign` **9.\*** till **v2.0.3.RELEASE** and uses **10.\*** after. Anyway, the dependency already has suitable `feign-form` version, see [dependency pom](https://github.com/spring-cloud/spring-cloud-openfeign/blob/master/spring-cloud-openfeign-dependencies/pom.xml#L19), so you don't need to specify it separately;

- `spring-cloud-starter-feign` is a **deprecated** dependency and it always uses the `OpenFeign`'s **9.\*** versions.

## Usage

Add `FormEncoder` to your `Feign.Builder` like so:

```java
SomeApi github = Feign.builder()
.encoder(new FormEncoder())
.target(SomeApi.class, "http://api.some.org");
```

Moreover, you can decorate the existing encoder, for example JsonEncoder like this:

```java
SomeApi github = Feign.builder()
.encoder(new FormEncoder(new JacksonEncoder()))
.target(SomeApi.class, "http://api.some.org");
```

And use them together:

```java
interface SomeApi {

@RequestLine("POST /json")
@Headers("Content-Type: application/json")
void json (Dto dto);

@RequestLine("POST /form")
@Headers("Content-Type: application/x-www-form-urlencoded")
void from (@Param("field1") String field1, @Param("field2") String[] values);
}
```

You can specify two types of encoding forms by `Content-Type` header.

### application/x-www-form-urlencoded

```java
interface SomeApi {

@RequestLine("POST /authorization")
@Headers("Content-Type: application/x-www-form-urlencoded")
void authorization (@Param("email") String email, @Param("password") String password);

// Group all parameters within a POJO
@RequestLine("POST /user")
@Headers("Content-Type: application/x-www-form-urlencoded")
void addUser (User user);

class User {

Integer id;

String name;
}
}
```

### multipart/form-data

```java
interface SomeApi {

// File parameter
@RequestLine("POST /send_photo")
@Headers("Content-Type: multipart/form-data")
void sendPhoto (@Param("is_public") Boolean isPublic, @Param("photo") File photo);

// byte[] parameter
@RequestLine("POST /send_photo")
@Headers("Content-Type: multipart/form-data")
void sendPhoto (@Param("is_public") Boolean isPublic, @Param("photo") byte[] photo);

// FormData parameter
@RequestLine("POST /send_photo")
@Headers("Content-Type: multipart/form-data")
void sendPhoto (@Param("is_public") Boolean isPublic, @Param("photo") FormData photo);

// Group all parameters within a POJO
@RequestLine("POST /send_photo")
@Headers("Content-Type: multipart/form-data")
void sendPhoto (MyPojo pojo);

class MyPojo {

@FormProperty("is_public")
Boolean isPublic;

File photo;
}
}
```

In the example above, the `sendPhoto` method uses the `photo` parameter using three different supported types.

* `File` will use the File's extension to detect the `Content-Type`;
* `byte[]` will use `application/octet-stream` as `Content-Type`;
* `FormData` will use the `FormData`'s `Content-Type` and `fileName`;
* Client's custom POJO for grouping parameters (including types above).

`FormData` is custom object that wraps a `byte[]` and defines a `Content-Type` and `fileName` like this:

```java
FormData formData = new FormData("image/png", "filename.png", myDataAsByteArray);
someApi.sendPhoto(true, formData);
```

### Spring MultipartFile and Spring Cloud Netflix @FeignClient support

You can also use Form Encoder with Spring `MultipartFile` and `@FeignClient`.

Include the dependencies to your project's pom.xml file:

```xml
<dependencies>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
```

```java
@FeignClient(
name = "file-upload-service",
configuration = FileUploadServiceClient.MultipartSupportConfig.class
)
public interface FileUploadServiceClient extends IFileUploadServiceClient {

public class MultipartSupportConfig {

@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;

@Bean
public Encoder feignFormEncoder () {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
}
```

Or, if you don't need Spring's standard encoder:

```java
@FeignClient(
name = "file-upload-service",
configuration = FileUploadServiceClient.MultipartSupportConfig.class
)
public interface FileUploadServiceClient extends IFileUploadServiceClient {

public class MultipartSupportConfig {

@Bean
public Encoder feignFormEncoder () {
return new SpringFormEncoder();
}
}
}
```

Thanks to [tf-haotri-pham](https://github.com/tf-haotri-pham) for his feature, which makes use of Apache commons-fileupload library, which handles the parsing of the multipart response. The body data parts are held as byte arrays in memory.

To use this feature, include SpringManyMultipartFilesReader in the list of message converters for the Decoder and have the Feign client return an array of MultipartFile:

```java
@FeignClient(
name = "${feign.name}",
url = "${feign.url}"
configuration = DownloadClient.ClientConfiguration.class
)
public interface DownloadClient {

@RequestMapping("/multipart/download/{fileId}")
MultipartFile[] download(@PathVariable("fileId") String fileId);

class ClientConfiguration {

@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;

@Bean
public Decoder feignDecoder () {
List<HttpMessageConverter<?>> springConverters =
messageConverters.getObject().getConverters();

List<HttpMessageConverter<?>> decoderConverters =
new ArrayList<HttpMessageConverter<?>>(springConverters.size() + 1);

decoderConverters.addAll(springConverters);
decoderConverters.add(new SpringManyMultipartFilesReader(4096));

HttpMessageConverters httpMessageConverters = new HttpMessageConverters(decoderConverters);

return new SpringDecoder(new ObjectFactory<HttpMessageConverters>() {

@Override
public HttpMessageConverters getObject() {
return httpMessageConverters;
}
});
}
}
}
```
Loading
Loading