Retrofit integration
Retrofit is a library that simplifies the access to RESTful services by turning an HTTP API into a Java interface.
Armeria provides a builder class called ArmeriaRetrofit
that builds an alternative
Retrofit
implementation that replaces the networking engine of Retrofit from OkHttp
to Armeria. By doing so, you get the following benefits:
- Better performance, thanks to Netty and its JNI-based I/O and TLS implementation
- Leverage other advanced features of Armeria, such as client-side load-balancing and service discovery
- Cleartext HTTP/2 support, as known as
h2c
First, you need armeria-retrofit2
dependency:
Gradle
Gradle (Kotlin)
Maven
build.gradle
dependencies {
implementation platform('com.linecorp.armeria:armeria-bom:1.31.0')
...
implementation 'com.linecorp.armeria:armeria-retrofit2'
}
Now, you can build Retrofit
using ArmeriaRetrofit.builder()
:
import com.linecorp.armeria.client.retrofit2.ArmeriaRetrofit;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
class UserInfo { ... }
interface MyService {
@GET("/userInfo/{id}")
CompletableFuture<UserInfo> getUserInfo(@Path("id") String id);
}
Retrofit retrofit = ArmeriaRetrofit.builder("http://localhost:8080/")
.addConverterFactory(JacksonConverterFactory.create())
.build();
MyService service = retrofit.create(MyService.class);
UserInfo userInfo = service.getUserInfo("foo").get();
For more information, please refer to the API documentation of the com.linecorp.armeria.client.retrofit2 package.