Last active
September 30, 2021 15:28
-
-
Save msmerc/4a53b51200bd1e80ce1e9f7ffe3efb16 to your computer and use it in GitHub Desktop.
Retrofit Polymophism Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonProperty; | |
import okhttp3.OkHttpClient; | |
import org.junit.jupiter.api.Test; | |
import retrofit2.Call; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.jackson.JacksonConverterFactory; | |
import retrofit2.http.Body; | |
import retrofit2.http.POST; | |
import java.io.IOException; | |
public class SimpleRetrofitTest { | |
public static interface Vehicle { | |
} | |
public static class Car implements Vehicle { | |
@JsonProperty("type") | |
public String type() { return "car"; } | |
} | |
public static class Lorry implements Vehicle { | |
@JsonProperty("type") | |
private String type() { return "car"; } | |
@JsonProperty("cargo") | |
private String cargo() { return "toys"; } | |
} | |
public interface Service { | |
@POST("/api/") | |
public Call<String> upload(@Body Car vehicle); | |
} | |
@Test | |
public void test() throws IOException { | |
final OkHttpClient client = new OkHttpClient(); | |
//client.interceptors().add(new FakeInterceptor()); | |
var retrofit = new Retrofit.Builder() | |
.addConverterFactory(JacksonConverterFactory.create()) | |
.baseUrl("http://whatever/") //We fail before we hit this! | |
.client(client) | |
.build(); | |
Service service = retrofit.create(Service.class); | |
var call = service.upload(new Car()); | |
call.execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment