1
在我的项目,我用了很多com.fasterxml.jackson.databind.ObjectMapper处理JSON,例如:为什么不使com.fasterxml.jackson.databind.ObjectMapper中的某些方法变为静态?
ObjectMapper mapper = new ObjectMapper();
A a = mapper.readValue(file.getBytes(), A.class);
乍一看,我认为这是更好,最好使readValue方法静态的,那么我们可以使用它作为ObjectMapper.readValue(),更清晰。但我知道肯定有一些理由不应该,有人知道吗?
看看ObjectMapper的其他构造函数。 ObjectMapper(com.fasterxml.jackson.core.JsonFactory jf)和ObjectMapper(JsonFactory jf,DefaultSerializerProvider sp,DefaultDeserializationContext dc)。 ObjectMapper类可以使用这些参数以多种方式进行配置。并且应用程序的每个部分都可以根据需要使用不同的配置。你不能真正做到这一点(你可以,但你最好不要)。这不是唯一的原因,但从可用性的角度来看。 –