简体   繁体   English

改造:用杰克逊反序列化失败,没有任何错误

[英]Retrofit: deserialization with jackson fails without any error

I have the following code that I use to call a Rest-API: service 我有以下用于调用Rest-API的代码:服务

Retrofit client = Client.getClient();
Call<ResponseBeanOutput<UserBeanOutput>> call = client.create(AuthResource.class).login("username","password","true");
Response<ResponseBeanOutput<UserBeanOutput>> response = call.execute();

The client is obteined via a static method: 客户端是通过静态方法来固执的:

public static Retrofit getClient() {
    ObjectMapper mapper = new ObjectMapper();
    return new Retrofit.Builder()
            .baseUrl(getBaseUrl())
            .addConverterFactory(JacksonConverterFactory.create(mapper))
            .build();
}

Server sends a response in JSON like this: 服务器使用JSON发送响应,如下所示:

{
    "error": false,
    "message": "Successfull login",
    "errorCode": 0,
    "data": {
        "uid": 7,
        "email": "user@example.com",
        "name": "username",
        "nick": "usernick",
        "photoUrl": "media/profiles/defaultImageProfile.png",
        "lts": "aaaaeqc9lf9od82rk633aaaaa"
    }
}

I have to map this response into a generic object ResponseBeanOutput that contains a field (data) of type UserBeanOutput (mapping the object "data" in json object). 我必须将此响应映射到一个通用对象ResponseBeanOutput中,该对象包含类型为UserBeanOutput的字段(数据)(映射json对象中的对象“ data”)。 So in the end ResponseBeanOutput will map fields "error","message","errorCode" and data will be an object of type "UserBeanOutput" containing "uid","email","nick", etc. 因此,最后,ResponseBeanOutput将映射字段“错误”,“消息”,“ errorCode”,并且数据将是“ UserBeanOutput”类型的对象,其中包含“ uid”,“ email”,“ nick”等。

My problem is that all objects are empty (null) and I have no errors. 我的问题是所有对象均为空(空),并且没有错误。

I tried also to remove generic type from ResponseBeanOutput and to map a response with a simple bean with fields "error" and "message" with releted getter and setter but without any success. 我还尝试从ResponseBeanOutput中删除泛型类型,并使用简单的bean(具有“错误”和“消息”字段)映射了响应,并删除了getter和setter,但没有成功。

What I am doing wrong? 我做错了什么?

I solved issue following suggestion of @Yevhenii Semenov, debugging execute() method. 我根据@Yevhenii Semenov的建议解决了问题,调试了execute()方法。 I found that the problem was in the API that didn't return body in the response and for this reason the deserialization was null. 我发现问题出在响应中没有返回正文的API中,因此反序列化为null。 The code for initialize the client was fine. 初始化客户端的代码很好。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM