I am making an external call via RestTemplate as follows:
ResponseEntity<Response> response = template.exchange("some.endpoint.com", HttpMethod.POST, request, MyClass.class);
The API returns a boolean value in String format as follows: ("0" or "1")
{
"some_lengthy_key_name" : "1"
}
I am trying to map this response to the following class.
@Getter
@JsonDeserialize(builder = MyClass.MyClassBuilder.class)
@Builder
public class MyClass{
@JsonProperty("some_lengthy_key_name")
private final boolean isValid;
}
It looks like Jackson doesn't entertain this and throws following error (understandable):
Can not deserialize value of type boolean from String "1" only "true" or "false" recognized.
I don't want to go down the route of capturing it as a String and then modifying value after. Instead prefer to go for the option of getting a custom deserialization going and went for the following:
public class Deserializer extends JsonDeserializer<Boolean> {
@Override
public Boolean deserialize(JsonParser parser, DeserializationContext context) throws IOException {
return !"0".equals(parser.getText());
}
}
I've now annotated the field in MyClass as follows:
@Getter
@JsonDeserialize(builder = MyClass.MyClassBuilder.class)
@Builder
public class MyClass{
@JsonDeserialize(using = Deserializer.class)
@JsonProperty("some_lengthy_key_name")
private final boolean isValid
}
But unfortunately this is not working either and throwing same error. Could I get some advice as to what I am doing wrong with this custom deserialization? Thanks.