Open Feign Forms Extension for Spring

Simple encoder for Netflix Feign project, which adds forms support (urlencoded and multipart)

License

License

Categories

Categories

Feign Net HTTP Clients ORM Data
GroupId

GroupId

io.github.openfeign.form
ArtifactId

ArtifactId

feign-form-spring
Last Version

Last Version

3.8.0
Release Date

Release Date

Type

Type

jar
Description

Description

Open Feign Forms Extension for Spring
Simple encoder for Netflix Feign project, which adds forms support (urlencoded and multipart)

Download feign-form-spring

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.openfeign.form/feign-form-spring/ -->
<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form-spring</artifactId>
    <version>3.8.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.openfeign.form/feign-form-spring/
implementation 'io.github.openfeign.form:feign-form-spring:3.8.0'
// https://jarcasting.com/artifacts/io.github.openfeign.form/feign-form-spring/
implementation ("io.github.openfeign.form:feign-form-spring:3.8.0")
'io.github.openfeign.form:feign-form-spring:jar:3.8.0'
<dependency org="io.github.openfeign.form" name="feign-form-spring" rev="3.8.0">
  <artifact name="feign-form-spring" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.openfeign.form', module='feign-form-spring', version='3.8.0')
)
libraryDependencies += "io.github.openfeign.form" % "feign-form-spring" % "3.8.0"
[io.github.openfeign.form/feign-form-spring "3.8.0"]

Dependencies

compile (4)

Group / Artifact Type Version
io.github.openfeign.form : feign-form jar 3.8.0
org.springframework : spring-web jar 5.1.5.RELEASE
commons-fileupload : commons-fileupload jar 1.4
org.slf4j : slf4j-api jar 1.7.26

provided (5)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.18.6
io.github.openfeign : feign-core jar 10.2.0
net.jcip : jcip-annotations jar 1.0
com.github.spotbugs : spotbugs-annotations jar 3.1.12
com.google.code.findbugs : jsr305 jar 3.0.2

test (9)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-web jar 2.1.3.RELEASE
org.springframework.cloud : spring-cloud-starter-openfeign jar 2.1.1.RELEASE
io.github.openfeign : feign-jackson jar 10.2.0
org.springframework.boot : spring-boot-starter-test jar 2.1.3.RELEASE
org.junit.jupiter : junit-jupiter-engine jar 5.4.1
org.junit.jupiter : junit-jupiter-params jar 5.4.1
org.assertj : assertj-core jar 3.12.2
org.mockito : mockito-core jar 2.25.1
org.mockito : mockito-junit-jupiter jar 2.25.1

Project Modules

There are no modules declared in this project.

Form Encoder

build_status maven_central License

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:

<dependencies>
  ...
  <dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form</artifactId>
    <version>3.8.0</version>
  </dependency>
  ...
</dependencies>

Gradle:

compile 'io.github.openfeign.form:feign-form:3.8.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 uses OpenFeign 9.* till v2.0.3.RELEASE and uses 10.* after. Anyway, the dependency already has suitable feign-form version, see dependency pom, 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:

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:

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

And use them together:

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

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

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:

  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:

<dependencies>
  <dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form</artifactId>
    <version>3.8.0</version>
  </dependency>
  <dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form-spring</artifactId>
    <version>3.8.0</version>
  </dependency>
</dependencies>
@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:

@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 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:

@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;
        }
      });
    }
  }
}
io.github.openfeign.form

Versions

Version
3.8.0
3.7.0
3.6.0
3.5.0
3.4.1
3.4.0
3.3.0
3.2.2
3.0.3
3.0.1
3.0.0
2.2.1
2.2.0
2.1.0
2.0.9
2.0.8
2.0.7
2.0.6
2.0.5
2.0.4
2.0.2