SoFunction
Updated on 2025-03-02

Lightweight declarative Http library——Feign's independent use

cutting edge

In projects, we often use HTTP tools to send requests to external REST interfaces. We usually use Okhttp or java's HttpClient to initiate it. Today we will introduce to you a lightweight declarative Http library (FeignClient). It will make our project code cleaner and facilitate maintenance!

Start quickly

Feign is a calling framework for the service consumer side in spring cloud, which is usually used in combination with ribbon, hystrix, etc. However, in some projects, for legacy reasons, the entire system is not a spring cloud project, or even a spring project, and the focus of users is only to simplify the writing of http calling code. If you use relatively heavier frameworks such as httpclient or okhttp, the encoding amount and learning curve will be a challenge for beginners. However, using RestTemplate in spring, there is no configuration solution. From this, you will think of whether you can use Feign independently without spring cloud.

Introduce dependencies

      <dependency>
            <groupId></groupId>
            <artifactId>feign-core</artifactId>
            <version>8.18.0</version>
        </dependency>
        <!-- /artifact//feign-gson -->
        <dependency>
            <groupId></groupId>
            <artifactId>feign-gson</artifactId>
            <version>8.18.0</version>
        </dependency>

Define interface

public interface OuterService {
    
    @RequestLine("GET /requesr/list?name={name}")
    @Headers("Authorization: Basic  {token}")
    String request(@Param(value = "name") String name,@Param(value = "token") String token);
}

Specify the HTTP request method and URL address through @RequestLine, and @Param specifies parameters. You can use {parameter name} in the url or header to fill in the request parameters.

Configuration class

OuterService service = ()
            .options(new Options(1000, 3500))
            .retryer(new (5000, 5000, 3))
            .target(, http://127.0.0.1:8085);

Start calling

("test","ad12hj3bhj1b23hj1b2");

json serialization

In the project, the Http requests we initiate are often in json format. Feign also provides a json-based object conversion tool to facilitate us to interact directly in object form.

Specify header at the interface level:

@Headers({"Content-Type: application/json","Accept: application/json"})

Specify Encoder and Decoder

## Specify Gson serialization.  It is also possible to use Jackson serialization (introduce its dependencies)OuterService service = ()
                .encoder(new GsonEncoder())
                .decoder(new GsonDecoder())
                .options(new Options(1000, 3500))
                .retryer(new (5000, 5000, 3))
                .target(, http://127.0.0.1:8085);

Support us to complete declarative Http interface calls

Using interceptor

You can customize interceptors when configuring proxy classes

OuterService service = ()
                .encoder(new GsonEncoder())
                .decoder(new GsonDecoder())
                .requestInterceptor(template -&gt; {
                    // template can obtain/modify body, header and other information                })
                .options(new Options(1000, 3500))
                .retryer(new (5000, 5000, 3))
                .target(, http://127.0.0.1:8085);

Detailed explanation of the annotation

@RequestLine("GET /messages/detail")

Specify the request method and request URL

    ## get request    @RequestLine("GET /messages/detail?msg_ids={msgIds}")
    ## Post Request    @RequestLine("POST /messages/detail?msg_ids={msgIds}")

@Param("name")

To bind parameter alias, you can get the value through {parameter name} in the annotations such as RequestLine/body/headers.

@Headers({"Accept:/", "Accept-Language: zh-cn"})

Specify the request header

@Body("{name}")

Specify the request return body value as the request parameter name

@QueryMap

Only marked on method parameters. Used to pass multiple query values, spliced ​​behind the URL, and can only be marked in front of Map-type parameters, otherwise an error will be reported.

@HeaderMap

Same as above, just used on the header

The above is the detailed content of the lightweight declarative Http library - the use of Feign. For more information about the use of Feign, please pay attention to my other related articles!