Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么不在com.fasterxml.jackson.databind.ObjectMapper中创建一些静态方法?_Java_Json_Jackson - Fatal编程技术网

Java 为什么不在com.fasterxml.jackson.databind.ObjectMapper中创建一些静态方法?

Java 为什么不在com.fasterxml.jackson.databind.ObjectMapper中创建一些静态方法?,java,json,jackson,Java,Json,Jackson,在我的项目中,我使用了很多com.fasterxml.jackson.databind.ObjectMapper来处理JSON,例如: ObjectMapper mapper = new ObjectMapper(); A a = mapper.readValue(file.getBytes(), A.class); 乍一看,我认为最好是将readValue方法设置为静态,然后我们可以将其用作ObjectMapper.readValue(),更清晰。但我知道一定有一些原因不应该,有人知道吗?另

在我的项目中,我使用了很多com.fasterxml.jackson.databind.ObjectMapper来处理JSON,例如:

ObjectMapper mapper = new ObjectMapper();
A a = mapper.readValue(file.getBytes(), A.class);

乍一看,我认为最好是将readValue方法设置为静态,然后我们可以将其用作ObjectMapper.readValue(),更清晰。但我知道一定有一些原因不应该,有人知道吗?

另一个原因是,在序列化和反序列化过程中,ObjectMapper为每个ObjectMapper实例绑定了某些特定的混淆,而ObjectMapper准备了这些混淆。 所以,在对象转换过程中,您可以配置不同的开始类型

 ObjectMapper mapper = new ObjectMapper();
   mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
   String json = mapper.writeValueAsString(new MyBean());

看看ObjectMapper的其他构造函数。ObjectMapper(com.fasterxml.jackson.core.JsonFactory jf)和ObjectMapper(JsonFactory jf、DefaultSerializerProvider sp、DefaultDeserializationContext dc)。可以使用这些参数以多种方式配置ObjectMapper类。应用程序的每个部分都可以根据需要使用不同的配置。你不能静态地做那件事(你可以,但最好不要)。这不是唯一的原因,而是从可用性的角度来看。