All Projects → OpenFeign → Feign Form

OpenFeign / Feign Form

Licence: apache-2.0
Open Feign form encoder

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Feign Form

Eve Sqlalchemy
SQLAlchemy data layer for Eve-powered RESTful APIs
Stars: ✭ 215 (-7.73%)
Mutual labels:  rest
Digital Restaurant
DDD. Event sourcing. CQRS. REST. Modular. Microservices. Kotlin. Spring. Axon platform. Apache Kafka. RabbitMQ
Stars: ✭ 222 (-4.72%)
Mutual labels:  rest
Flask Restplus
Fully featured framework for fast, easy and documented API development with Flask
Stars: ✭ 2,585 (+1009.44%)
Mutual labels:  rest
Spring Cloud Study
SpringCloud源码实战微服务框架(202003更新到SpringCloud的Greenwich版本,即基于SpringBoot2.1.7版本,以及新增SpringCloud Alibaba从入门到精通教程),涉及内容:配置中心,注册中心,服务提供者,服务消费者,熔断器,配置服务,API Gateway,API网关限流ratelimit,服务链路追踪zipkin,hystrix监控,监控聚合中心turbine等
Stars: ✭ 215 (-7.73%)
Mutual labels:  feign
Spring Hateoas Examples
Collection of examples on how (and why) to build hypermedia-driven apps with Spring HATEOAS
Stars: ✭ 219 (-6.01%)
Mutual labels:  rest
Mastering Junit5
A comprehensive collection of test examples created with JUnit 5
Stars: ✭ 223 (-4.29%)
Mutual labels:  rest
Tutorial
Spring Boot的例子,包含RESTful API, MVC, JMS, Cache, Mybatis, Cache, Websocket...
Stars: ✭ 215 (-7.73%)
Mutual labels:  rest
Horaires Ratp Api
Webservice pour les horaires et trafic RATP en temps réel
Stars: ✭ 232 (-0.43%)
Mutual labels:  rest
Serverest
APIs REST simulando loja virtual para servir de estudo de testes de API de forma manual ou automatizada
Stars: ✭ 223 (-4.29%)
Mutual labels:  rest
Node Hbase
Asynchronous HBase client for NodeJs using REST
Stars: ✭ 226 (-3%)
Mutual labels:  rest
Restito
Restito - mocking framework for testing rest clients
Stars: ✭ 217 (-6.87%)
Mutual labels:  rest
Ccxt Rest
Open Source Unified REST API of 100+ Crypto Exchange Sites (18k+ docker pulls) - https://ccxt-rest.io/
Stars: ✭ 210 (-9.87%)
Mutual labels:  rest
Rested
A REST client for browsers
Stars: ✭ 225 (-3.43%)
Mutual labels:  rest
Springboot Examples
spring boot 实践系列
Stars: ✭ 216 (-7.3%)
Mutual labels:  rest
Spife
🍛 🍴 a jshttp based µframework 🍴 🍝
Stars: ✭ 230 (-1.29%)
Mutual labels:  rest
Ray
a framework that helps you to deliver well-designed python APIs
Stars: ✭ 215 (-7.73%)
Mutual labels:  rest
Spring Dubbo Service
微服务 spring dubbo项目:dubbo rpc;druid数据源连接池;mybatis配置集成,多数据源;jmx监控MBean;定时任务;aop;ftp;测试;Metrics监控;参数验证;跨域处理;shiro权限控制;consul服务注册,发现;redis分布式锁;SPI服务机制;cat监控;netty服务代理;websocket;disconf;mongodb集成;rest;docker;fescar
Stars: ✭ 224 (-3.86%)
Mutual labels:  rest
Bear.sunday
A resource-oriented application framework
Stars: ✭ 230 (-1.29%)
Mutual labels:  rest
Elastix
A simple Elasticsearch REST client written in Elixir.
Stars: ✭ 231 (-0.86%)
Mutual labels:  rest
Guzzle Services
Provides an implementation of the Guzzle Command library that uses Guzzle service descriptions to describe web services, serialize requests, and parse responses into easy to use model structures.
Stars: ✭ 226 (-3%)
Mutual labels:  rest

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;
        }
      });
    }
  }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].