简体   繁体   中英

Retrofit: deserialization with jackson fails without any error

I have the following code that I use to call a Rest-API: service

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:

{
    "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). 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.

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.

What I am doing wrong?

I solved issue following suggestion of @Yevhenii Semenov, debugging execute() method. 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. The code for initialize the client was fine.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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