Do you know why when debbuging my app the order of the attributes of my address object is changed? I need to check it the address stored in the database is equal to the one coming from the frontend. I'm using objectmapper to get the object into text so the I can compare 2 strings but due this situation I always get false as the order change as you can see in the picture below.
CodePudding user response:
Using new ObjectMapper.writeValueAsString()
should give you the fields in order as you expect it to.
As @Federico klez Culloca stated, you should instead use the objects themselves to compare.
You should construct an Address
object from address.getAddressString()
or just directly compare address
to a
using a.equals()
.
CodePudding user response:
You can parse strings to JsonNode
and compare them. Jackson provides overrides of equals(Object o)
for all node types.
ObjectMapper mapper = new ObjectMapper();
JsonNode actualNode = mapper.readTree(actualJson);
JsonNode expectedNode = mapper.readTree(expectedJson));
boolean matches = actualNode.equals(expectedNode);
This guide should be helpful as well.
The other option is to provide implementation of equals()
in you custom class, as mentioned in comments.