Reputation: 973
I have a request executor class that sends request to a webservice and then reads the responses and creates a java object from the response using ObjectMapper.readValue();
My problem is that the webservice returns the responses in mixedCase and not in the correct CamelCase for the classes in the response so for example the following response:
{
"serviceResponse": {
"header": {
"success": "false",
"dateTime": "2012-03-12 09:06:45.60 UTC",
"errorCodes": [
{
"code": "123",
"message": "error occured while trying to get response - User not Logged in",
"causeString": "css_idks"
}
]
},
"body": {
"cls": "lst",
"empty": {}
}
}
}
My class structure is as follows: ServiceResponse.Java:
public class ServiceResponse implements Serializable {
public ResponseHeader header;
public ResponseBody body;
}
I also have classes for ResponseHeader.java and ResponseBody.java which are similar. The problem is that while the header field unmarshalls to the ResponseHeader object correctly because the parameter name is indeed 'header', the ServiceResponse fails since the class name starts with a capital S and not a lowercase s.
I get the following exception when trying to parse the response:
03-12 11:14:14.078: E/ELAD(3473): org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "serviceResponse" (Class com.elad.ServiceResponse), not marked as ignorable
and if I add the ignorable=true annotation, it just parses it but everything inside is null...
I need to know how to annotate the class so that I can define a field name that it should map to which is different from the name of the class in the same way I am able to do for the fields\methods using the @JsonProperty annotation.
Note that I cannot change the request executor so I can't put in a different de-serializer or something like that.
Upvotes: 1
Views: 1945
Reputation: 973
Ok, so I just made a silly mistake, but in the hopes that this will help others, I'll leave the question on with my answer: I should not have used the ServiceResponse as the class passed to objectMapper.readValue(), but rather a DTO that contains it. I did it for all other data types and for some reason forgot to do it here.
my bad... :)
Upvotes: 2