Quarkus Easy Retrofit

Describe what the extension does here.

Quarkus Easy Retrofit is an extension of the easy-retrofit library in Quarkus. This extension is based on the ability of Quarkus Arc container dependency injection, which implicitly injects retrofit object instances into the container, simplifying the creation code of retrofit in use

easy-retrofit, which is a specialized library that provides retrofit injection for various web frameworks and is based on OkHttp3 interceptors Provide extension.

Installation

If you want to use this extension, you need to add the io.quarkiverse.retrofit:quarkus-easy-retrofit extension first to your build file.

For instance, with Maven, add the following dependency to your POM file:

<dependency>
    <groupId>io.quarkiverse.retrofit</groupId>
    <artifactId>quarkus-easy-retrofit</artifactId>
    <version>1.1.2</version>
</dependency>

Usage

Add @EnableRetrofit Annotation

The @EnableRetrofit Annotation will enable to use quarkus-easy-retrofit

if your application has main class, you can add @EnableRetrofit Annotation to your main class.

@EnableRetrofit("xxx.yyy.zzz")
@QuarkusMain
public class QuarkusRetrofitClientDemo {
    public static void main(String[] args) {
    Quarkus.run(RetrofitDemoApplication.class, args);
    }

    public static class RetrofitDemoApplication implements QuarkusApplication {
        @Override
        public int run(String... args) throws Exception {
            System.out.println("Do startup logic here");
            Quarkus.waitForExit();
            return 0;
        }
    }
}

if your application has no main class, you can add @EnableRetrofit Annotation to your resource class.

@EnableRetrofit("xxx.yyy.zzz")
@Path("/easy-retrofit")
@ApplicationScoped
public class EasyRetrofitResource{

}

You need specify basePackages like @EnableRetrofit(basePackages = "xxx.yyy.zzz"), "xxx.yyy.zzz" is your retrofit APIs folder name.

Create an Interface file, and use @RetrofitBuilder

@RetrofitBuilder will create a Retrofit.Builder() object, and it will be managed by Quarkus Arc container

baseUrl can be a URL string or a properties in a resource file

@RetrofitBuilder(baseUrl = "${quarkus.retrofit.baseUrl}")
public interface BaseApi {

    @GET("api/hello")
    Call<ResponseBody> hello();
}

and application.properties file

quarkus.retrofit.baseUrl=http://localhost:8080

Use Retrofit API in Resource

Use @Inject to inject API Interface, the quarkus-easy-retrofit will help you to create instance of API Interface file.

@Path("/v1/hello")
@ApplicationScoped
public class EasyRetrofitResource {

    @Inject
    BaseApi baseApi;

    @GET
    public String hello() throws IOException {
        Call<ResponseBody> hello = baseApi.hello();
        return hello.execute().body().string();
    }
}

Yes, Congratulations, your code should work normally.

The current document is being improved. For more comprehensive usage methods, please refer to the documentation of easy-retrofit-spring-boot -starter first. Of course, please note to replace Quarkus @Inject and Springboot @Resource @Autowired