[IntelliJ IDEA] Using forest-spring-boot-starter to send HTTP by calling the local interface method

forest-spring-boot-starter

What is Forest?

Forest is an open source Java HTTP client framework. It can bind all HTTP request information (including URL, Header, Body and other information) to your custom Interface method, and send HTTP by calling the local interface method.

Why use Forest?

Using Forest is like using an RPC framework like Dubbo. You only need to define the interface and call the interface. You don’t need to care about the details of sending HTTP requests. At the same time, the HTTP request information is decoupled from the business code, so that you can manage a large number of HTTP URLs, headers and other information in a unified manner. The caller of the request does not need to care about the specific content of the HTTP at all. Even if the HTTP request information changes, in most cases there is no need to modify the code that calls and sends the request.

How to use Forest?

Forest does not require you to write a specific HTTP call process, only you need to define an interface, and then add the information of the HTTP request to the interface method through Forest annotations. The request sender can automatically send the request and accept the response of the request by calling the interface defined by you.

How Forest Works

Forest will generate a specific implementation class for the interface you defined through dynamic proxy, then organize and verify HTTP request information, bind dynamic data, convert data form, SSL verification signature, and call back-end HTTP API (httpclient and other APIs) ) Dirty tasks such as executing the actual request, waiting for the response, failing to retry, converting the response data to the Java type, etc. are all packaged by the implementation class of this dynamic agent. When requesting the sender to call this interface, it is actually calling this implementation class that does a lot of work.

Features

  • Take Httpclient and OkHttp as the back-end framework

  • By calling local methods to send Http requests, the decoupling between business logic and Http protocol is realized

  • Because of the third-party interface, there is no need to rely on Spring Cloud and any registry

  • Support all request methods: GET, HEAD, OPTIONS, TRACE, POST, DELETE, PUT, PATCH

  • Support file upload and download

  • Supports flexible template expressions

  • Support each life cycle of interceptor processing request

  • Support custom annotations

  • Support OAuth2 authentication

  • Support filters to filter the incoming data

  • Define Http request based on annotation and configuration

  • Support Spring and Springboot integration

  • Automated parsing of JSON strings to Java objects

  • Automatic parsing of XML text to Java objects

  • JSON, XML or other type converters can be expanded and replaced at will

  • Support JSON conversion framework: Fastjson, Jackson, Gson

  • Supports JAXB format XML conversion

  • The callback of the request result can be realized through the OnSuccess and OnError interface parameters

  • The configuration is simple, generally only a @Request annotation is needed to complete the definition of most requests

  • Support asynchronous request call

Prerequites

Java Development Kit (JDK) version

Forest 1.2.x and above are based on JDK 1.8.

Dependencies

Gradle

1
2
3
4
5
6
7
8
9
10
11

sourceCompatibility = '8'

dependencies {
// https://mvnrepository.com/artifact/com.dtflys.forest/forest-spring-boot-starter
implementation group: 'com.dtflys.forest', name: 'forest-spring-boot-starter', version: '1.5.2-BETA3'

// Jackson
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-json
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-json', version: '2.5.4'
}

Maven

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<dependency>
<groupId>com.dtflys.forest</groupId>
<artifactId>forest-spring-boot-starter</artifactId>
<version>1.5.2-BETA3</version>
</dependency>

<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.10</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.10</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.10</version>
</dependency>

Getting Start

Create an interface, such as named AmapClient decorated with @BaseRequest annotation, and create an interface method named getLocation, decorated with @Get annotation.

AmapClient.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.cloudolife.examples.forest.clients;

import com.dtflys.forest.annotation.BaseRequest;
import com.dtflys.forest.annotation.Get;

import java.util.Map;

@BaseRequest(sslProtocol = "TLS")
public interface AmapClient {

// The ${0} in the url represents the first parameter, and ${1} refers to the second parameter.
@Get("https://ditu.amap.com/service/regeo?longitude=${0}&latitude=${1}")
Map getLocation(String longitude, String latitude);
}

Through the @BaseRequest and @Get annotation, the getLocation() method in the AmapClient interface above is bound to an HTTP request, the URL is https://ditu.amap.com/service/regeo?longitude=${0}&latitude=${1} and the request response data is Map The way is returned to the caller.

Call interface in Spring Boot project

Just add the @ForestScan annotation to the Spring Boot configuration class or startup class, and fill in the package name of the remote interface in the basePackages property

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.cloudolife.examples.forest;

import com.dtflys.forest.springboot.annotation.ForestScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@ForestScan(basePackages = "com.cloudolife.examples.forest.clients")
public class ClouodoLifeBiJavaForestApplication {

public static void main(String[] args) {
SpringApplication.run(ClouodoLifeBiJavaForestApplication.class, args);
}

}

Call interface within Test.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.cloudolife.examples.forest.cients;

import com.cloudolife.examples.forest.clients.AmapClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Map;

@SpringBootTest
public class AmapClientTest {

@Autowired
private AmapClient amapClient;

@Test
public void testGetLocation() {
Map result = amapClient.getLocation("121.475078", "31.223577");
System.out.println(result);
}
}

JSON parsing framework

If your project does not yet have a JSON parsing framework, such as Jackson, please add the following dependencies as needed. If you already have one, you can skip this step.

1
2
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-json
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-json', version: '2.5.4'
1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-json -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.5.4</version>
</dependency>

References

[1] dromara/forest: A high-level and lightweight HTTP client framework for Java. it makes sending HTTP requests in Java easier. - https://github.com/dromara/forest

[2] Forest官方网站 - 轻量级HTTP框架 | Forest - http://forest.dtflyx.com/

[3] Maven Repository: org.springframework.boot » spring-boot-starter-json - https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-json

[4] Spring Boot - https://spring.io/projects/spring-boot