-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@SerializedName bug in Java when it's used with Retrofit2 library #2137
Comments
Is this the issue you are seeing, Maybe the problem here is that you have not configured Spring Boot to use Gson, so when your method |
Hello,
If i replace @SerializedName with @JsonProperty which is using the Jackson Serialization way then I call the respone.body(), i'll get the bookNameTest correctly (even if it's calling the toString method as you mentioned). But it's not the case for Gson with @SerializedName, the response.body() as you said it will always give me the "bookName". Hope my explanation is clearer :) |
How exactly have you configured Spring Boot? I am not very familiar with it, but tutorials such as this one or this (section "3. Make Gson preferred json mapper") suggest that it might be necessary to adjust the configuration to make Spring Boot prefer Gson over Jackson. Maybe this StackOverflow question is helpful as well.
That might be special handling in Lombok for Jackson, but I assume it does not matter for the JSON serialization. |
Hi Marco
For Lombok, i have checked the generated classes toString for both @SerializedName and @JsonProperty i don't see any dfiference. So it's kind of confusing me for the Gson behavior, as you could see the Serialized String from println is correct, but if calling the response.body() to get Object, it's not as I expected the library will behave |
Hi Marco, i have found the issue, it's from SpringBoot configuration. I need to add a new config inside application.yml to force it to use Gson
Thank for your time. I think i'll close the issue as it's not a bug it's SpringBoot configuration :) |
Gson version
2.9.0
Java / Android version
java 17
Used tools
-Maven wrapper 3.8.4
Description
@SerializedName is not properly serialized in object.The deserialization from URL json to object is ok but not the serialization. If i use @JsonProperty the name is correctly serialized.
Expected behavior
I expect the SerializedName should be used which is "bookNameTest" when I get the return Object but it's not, it's only printed out correctly in the std but if you get the object as return, the response is not correct
@SerializedName("bookNameTest")
private String bookName;
Actual behavior
{
retrofit2.Response response = bookClient.getBook("api/v1/bookService/otherBooks").execute();
return response.body();
}
=> Wrong behavior here, i expect "bookNameTest": "JWT" but it's printed "bookName"="JWT"
Reproduction steps
The mock Json response from URL endpoint "api/v1/bookService/otherBooks"
Please note for bookClient I used Retrofit and use Gson converter. And then the deserialization is correctly done but after that the serialization to use the @SerializedName is not correct.
I have checked the jsonString is printed out correctly from console using System.out.println(jsonString)
but the return response.body() is not
Book(bookId=1, price=22.25, bookName=JWT}
====================================================
If inside Book.java, i used @JsonProperty, the book can be deserialized and serialized correctly. With using the same code, the return object, it will use @JsonProperty name which is "bookNameTest"
Correct output
Exception stack trace
The text was updated successfully, but these errors were encountered: