A library adopt Retrofit 2 and OkHttp as a REST client with proxy.
Retrofit2 - A type-safe HTTP client for Android and Java
OKHttp - OkHttp is an HTTP client
Build and Import this library jar into your project
// Build
gradlew clean build
// Gradle import from your maven repository
repositories {
mavenCentral()
maven { url 'http://xxxxx.com/artifactory/libs' }
}
dependencies {
compile(group: 'com.gmail.oraclebox', name: 'helper-retrofit2', version: '1.0.0')
}
interface Api {
@FormUrlEncoded
@POST('oauth2/token')
Call<OAuthResponse> oauth(@Field("client_id") String clientId, @Field("client_secret") String clientSecret, @Field("grant_type") String grantType, @Field("resource") String resource);
@GET('greeting')
Call<Message> getTrailerOrders(@Header("Authorization") String accessToken);
// Remote Return String only
@GET('/')
Call<String> testGoogle();
}
Api api = RetrofitHelper.getRetofit(endpoint.uri, proxyHost, port, objectMapper).create(Api.class);
Response<Message> response = api.getTrailerOrders(accessToken).execute();
if (response.isSuccessful()) {
// Success
}
Api api = RetrofitHelper.getRetrofitStringConverter('https://www.google.com/', null, 0, new ObjectMapper()).create(Api.class);
Response<String> response = api.testGoogle().execute();
assertTrue(response.code() < 300);
assertNotNull(response.body());
Api api = RetrofitHelper.getUnsafeRetrofit('https://selfsign.com/', null, 0, new ObjectMapper(), new HttpLogger()).create(Api.class);
Response<Object> response = api.signInAccount(new SignInRequest(domainName: 'xxxxxxx', password: 'xxxxxx')).execute();
assertTrue(response.code() > 300);
The libaray use Jackson for Json serialization and deserialization, you can pass Spring's ObjectMapper to method.