2018年2月14日 | Leave a comment 声明式 restful 接口 解决: App.java package com.enjoylearn.retrofit1; import java.io.IOException; import java.util.List; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.jackson.JacksonConverterFactory; /** * * @author sihai */ public class App { public static void main(String[] argv) throws IOException { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .addConverterFactory(JacksonConverterFactory.create()) .build(); GitHubService service = retrofit.create(GitHubService.class); Response<List<Repo>> repos = service.listRepos("giant35").execute(); repos.body().forEach(r -> System.out.println(r)); } } 1234567891011121314151617181920212223242526 package com.enjoylearn.retrofit1; import java.io.IOException;import java.util.List;import retrofit2.Response;import retrofit2.Retrofit;import retrofit2.converter.jackson.JacksonConverterFactory; /** * * @author sihai */public class App { public static void main(String[] argv) throws IOException { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .addConverterFactory(JacksonConverterFactory.create()) .build(); GitHubService service = retrofit.create(GitHubService.class); Response<List<Repo>> repos = service.listRepos("giant35").execute(); repos.body().forEach(r -> System.out.println(r)); } } Repo.java package com.enjoylearn.retrofit1; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; /** * * @author sihai */ @JsonIgnoreProperties(ignoreUnknown = true) public class Repo { private Long id; private String name; private String fullName; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } @Override public String toString() { return "Repo{" + "id=" + id + ", name=" + name + ", fullName=" + fullName + '}'; } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445 package com.enjoylearn.retrofit1; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; /** * * @author sihai */@JsonIgnoreProperties(ignoreUnknown = true)public class Repo { private Long id; private String name; private String fullName; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } @Override public String toString() { return "Repo{" + "id=" + id + ", name=" + name + ", fullName=" + fullName + '}'; } } GitHubService.java package com.enjoylearn.retrofit1; import java.util.List; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Path; public interface GitHubService { /* https://api.github.com/users/giant35/repos */ @GET("users/{user}/repos") Call<List<Repo>> listRepos(@Path("user") String user); } 1234567891011121314 package com.enjoylearn.retrofit1; import java.util.List;import retrofit2.Call;import retrofit2.http.GET;import retrofit2.http.Path; public interface GitHubService { /* https://api.github.com/users/giant35/repos */ @GET("users/{user}/repos") Call<List<Repo>> listRepos(@Path("user") String user);}