Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsonMappingException NOT getting thrown when it should

Tags:

java

json

jackson

I have a class in Java that is generically typed. It is supposed to return an object of type T after receiving some json. I am using the following code to create the object:

ObjectMapper mapper = new ObjectMapper();
this.object = mapper.readValue(json, type);

This method throws a JsonMappingException, and should do so if the object isn't of the proper type. The problem I'm running into (when unit testing) is that if I pass in json of an incorrect type, as long as both objects are pojos no exception is being thrown. I am simply getting back an object of the correct type where all it's fields are null.

Why is the exception not getting thrown here? If I pass in some json with a bunch of fields that don't exist on the type of object it should be mapping to, shouldn't I get an exception from that?

like image 401
Craig Avatar asked Apr 24 '26 13:04

Craig


1 Answers

You possibly have:

@JsonIgnoreProperties(ignoreUnknown = true)

set somewhere, so jackson doesn't complain about the mismatch.

like image 79
beerbajay Avatar answered Apr 26 '26 03:04

beerbajay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!