Reputation: 2205
I'm calling a REST service using retrofit. The REST service returns a JSON message. The Java response object has null fields values.
Setup Code:
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.create();
RestAdapter restAdapter = new RestAdapter.Builder()
.setConverter(new GsonConverter(gson))
.setEndpoint(myEndpoint).build();
restAdapter.setLogLevel(LogLevel.FULL);
SomeService someService = restAdapter
.create(SomeService.class);
The service interface:
public interface SomeService {
@POST("/firstParam/{firstParam}/secondParam/{secondParam}")
MyResponse callService(@Path("firstParam") String firstParam,
@Path("secondParam") String secondParam, @Body MyRequest request);
}
The Response Pojo:
class MyResponse {
private String paramOne;
private String paramTwo;
private String action;
private String status;
//getters and setters
}
But when I call:
MyResponse response = someService.callService("one","two",request);
the fields in response are still null.
I had to use a custom JsonDeserializer to get it working but I'm thinking this is standard stuff it shouldn't need a JsonDeserializer.
Can you suggest an approach that saves me from using a JsonDeserializer ?
Upvotes: 1
Views: 619
Reputation: 13002
You set setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
when building your Gson
object.
Accordingly, Gson will now try to map the Java field name paramOne
to a value with key ParamOne
in your JSON. But no such value exists - it is called paramOne
, just like in the Java class.
So, try this:
setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
. (You can still use @SerializedName
in cases where you need a different mapping from JSON key to Java field name.)Upvotes: 2