ObjectMapper mining pit record and source code analysis

ObjectMapper mining pit record and source code analysis

Problem Description

When two environments share a private server, one environment is released first, and the latest code is pulled; the other environment is not released, there is no release environment, and the code does not contain the passed attributes, and an error will be reported.

Issue tracking

Track 1

ObjectMapper mining pit record and source code analysis
The above code is the latest code released by one environment, and the other environment is not released, resulting in no relevant attributes.

Track 2

ObjectMapper mining pit record and source code analysis
Remove the comment of objMap above, this case will be executed successfully, because the set attribute does not exist, no abnormal situation will be reported.

Source code analysis

ObjectMapper mining pit record and source code analysis
1. According to the English description, the default is true, as long as the attribute does not exist, an exception will be thrown, and it is generally set to false during normal development.
2. When designing and coding, consider the configuration of this class and possible exceptions
ObjectMapper objectMapper = new ObjectMapper();
//This is all the attributes of the serialized object
objectMapper.setSerializationInclusion(Include.ALWAYS);
//This It is the setting when there is no attribute
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//When the object is empty, don't throw exception
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//Time needs to be converted by yourself, the default is time Axis
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))

to sum up

1. Usually when developing and designing, especially public classes, there are several modules that are used. At this time, adding and deleting attributes should be fully tested, and the called api should also be carefully checked.
2. Although there are pits in the api called in normal times, there must be a solution. Don't change the api as soon as you encounter the pit. Maybe the changed api is also pitted. Another one will increase the development time, and the gain is not worth the loss.

Guess you like

Origin blog.51cto.com/xxdeelon/2550634