有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在改造成功和错误回调中使用不同的反序列化模型

我使用的API对成功和失败给出了完全不同的响应

成功:

{
   "token":"asdfasdfhkAADBSKJBJBJKBJBK^%&BJBLLKHKJBXZ",
   "email":"sample@sample.com",
   "role":"admin"
}

失败:

{
   "name": "NotAuthenticated",
   "message": "Invalid login.",
   "code": 401,
   "className": "not-authenticated"
}

我是一个非常新的改装和使用下面的代码打电话

LoginRequest request = new LoginRequest(mobileNumber, password);
    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

    Call<LoginResponse> call = apiService.authenticateUser(request);
    call.enqueue(new Callback<LoginResponse>() {
        @Override
        public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {


        }

        @Override
        public void onFailure(Call<LoginResponse> call, Throwable t) {

        }
    });

正如您所看到的,改装迫使我对successfailure使用样本ResponseObject。因此,我无法将失败响应转换为pojo

我已经研究了自定义反序列化。但是,为每个请求编写自定义反序列化程序可能会很快失控

请帮忙


共 (1) 个答案

  1. # 1 楼答案

    我认为最好的解决方案是让我们自己在GSON中对身体形态进行改装并序列化。我也在寻找其他解决方案