Andrey Arzhannikov Andrey Arzhannikov - 2 months ago 21
Android Question

validateEagerly() method in Retrofit 2.0 (beta 2)

What exactly does

validateEagerly()
(Retrofit 2.0 beta 2) method do?
Where can I apply this method?

JavaDoc says that
When calling create on the resulting Retrofit instance, eagerly validate the configuration of all methods in the supplied interface.
but this description is not so clear.

Answer

validateEagerly() validates the configuration you have provided when building your Retrofit instance. This will inspect your interface annotations for valid Retrofit annotations, inspection of arguments and method arguments, if they are consistent with the annotations set, also this checks for possible incorrect method return types (different than Call<T>).

The inspection is usually called before you build your Retrofit instance.

private Retrofit getRetrofit() {
        OkHttpClient okClient = getOkHttpClient();
        GsonBuilder builder = getGsonBuilder();
        Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
        retrofitBuilder.client(okClient);
        retrofitBuilder.baseUrl(API_ENDPOINT);
        retrofitBuilder.addConverterFactory(GsonConverterFactory.create(builder.create()));
        retrofitBuilder.validateEagerly();
        return retrofitBuilder.build();
    }

It can throw IllegalArgumentException with different message and cause, if something is not valid.

Comments