参考图谱

【20180928】SpringBoot   retrofit - 图1

注意事项

  • 按照官方教程处理,可能会碰到GsonConverterFactory.create()无法在IDEA中识别的问题,解决方案为pom引入依赖。注意两个版本号应该一致。
  1. <dependency>
  2. <groupId>com.squareup.retrofit2</groupId>
  3. <artifactId>retrofit</artifactId>
  4. <version>2.0.0-beta3</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.squareup.retrofit2</groupId>
  8. <artifactId>converter-gson</artifactId>
  9. <version>2.0.0-beta3</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.google.code.gson</groupId>
  13. <artifactId>gson</artifactId>
  14. <version>2.3</version>
  15. </dependency>

调试代码

  • 注意GsonConverterFactory.create()很可能报错(找不到类),如果报错了,检查自己的pom.xml引入是否正常。
  1. import retrofit2.Call;
  2. import retrofit2.GsonConverterFactory;
  3. import retrofit2.Retrofit;
  4. import retrofit2.http.GET;
  5. import retrofit2.http.Path;
  6. import java.io.IOException;
  7. import java.util.List;
  8. public class TestRetrofit {
  9. public static final String API_URL = "https://api.github.com";
  10. public static class Contributor {
  11. public final String login;
  12. public final int contributions;
  13. public Contributor(String login, int contributions) {
  14. this.login = login;
  15. this.contributions = contributions;
  16. }
  17. }
  18. public interface GitHub {
  19. @GET("/repos/{owner}/{repo}/contributors")
  20. Call<List<Contributor>> contributors(
  21. @Path("owner") String owner,
  22. @Path("repo") String repo);
  23. }
  24. public static void main(String... args) throws IOException {
  25. // Create a very simple REST adapter which points the GitHub API.
  26. Retrofit retrofit = new Retrofit.Builder()
  27. .baseUrl(API_URL)
  28. .addConverterFactory(GsonConverterFactory.create())
  29. .build();
  30. // Create an instance of our GitHub API interface.
  31. GitHub github = retrofit.create(GitHub.class);
  32. // Create a call instance for looking up Retrofit contributors.
  33. Call<List<Contributor>> call = github.contributors("square", "retrofit");
  34. // Fetch and print a list of the contributors to the library.
  35. List<Contributor> contributors = call.execute().body();
  36. for (Contributor contributor : contributors) {
  37. System.out.println(contributor.login + " (" + contributor.contributions + ")");
  38. }
  39. }
  40. }

运行结果

  1. JakeWharton (925)
  2. swankjesse (236)
  3. pforhan (48)
  4. eburke (36)
  5. dnkoutso (26)
  6. edenman (24)
  7. NightlyNexus (24)
  8. loganj (17)
  9. Noel-96 (16)
  10. rcdickerson (14)
  11. rjrjr (13)
  12. kryali (9)
  13. adriancole (9)
  14. holmes (7)
  15. swanson (7)
  16. JayNewstrom (6)
  17. crazybob (6)
  18. Jawnnypoo (6)
  19. danrice-square (5)
  20. Turbo87 (5)
  21. naturalwarren (5)
  22. vanniktech (4)
  23. guptasourabh04 (4)
  24. artem-zinnatullin (3)
  25. codebutler (3)
  26. icastell (3)
  27. jjNford (3)
  28. f2prateek (3)
  29. PromanSEW (3)
  30. koalahamlet (3)

遗留问题

  • 这种写法会不会有SSRF问题

  • 其他更复杂的用法实现(如POST,加Header等)

  • 现在是GsonConverterFactory.create(),但如果想和Jackson联动或者Fastjson联动,应该如何操作变更。

参考资料