相关疑难解决方法(0)

具有Retrofit 2的多个转换器

我有一个HATEOAS(HAL) REST服务并设法与下面的代码交谈(使用halarious作为转换引擎)但是当我尝试合并转换器(stallonestallone2)时,应用程序将总是拿起第一个转换器,而不是适合于响应类型的那个当然会导致错误.

我怎么能避免只在小型细节中有所不同的重复改造?

public interface Stallone {
   @GET("/discovery")
   Call<DiscoveryResponse> discover();
   @POST()
   Call<LoginResponse> login(@Url String url, @Body LoginRequest secret);
}
Run Code Online (Sandbox Code Playgroud)
   public static void main(String... args) throws IOException {
      // Initialize a converter for each supported (return) type
      final Stallone stallone = new Retrofit.Builder()
         .baseUrl(BASE)
         .addConverterFactory(HALConverterFactory.create(DiscoveryResponse.class))
         .build().create(Stallone.class);
      final Stallone stallone2 = new Retrofit.Builder()
         .baseUrl(BASE)
         .addConverterFactory(HALConverterFactory.create(LoginResponse.class))
         .build().create(Stallone.class);

      // Follow the HAL links
      Response<DiscoveryResponse> response = stallone.discover().execute();
      System.out.println(response.code() + " " + response.message());
      Assert.assertNotNull(response.body());
      String …
Run Code Online (Sandbox Code Playgroud)

java generics hateoas gson retrofit

17
推荐指数
1
解决办法
9459
查看次数

标签 统计

generics ×1

gson ×1

hateoas ×1

java ×1

retrofit ×1