不需要每次都声明Jackson的ObjectMapper吗?

huangapple go评论85阅读模式
英文:

Should I declare Jackson's ObjectMapper every time I need it?

问题

我有一个多线程的Java应用程序,我在其中使用了Jackson库。每当我需要ObjectMapper时,我会在需要使用它的类中声明它。有时我会使用objectMapper.configure()来自定义这个ObjectMapper,但并不总是这样做。
这是一种不好的做法吗?有没有办法只声明一两次就可以了呢?

英文:

I have a multithread Java application, where I use the Jackson library. Everytime I need the ObjectMapper I declare it in the classes where I need to use it. Sometimes I customize this ObjectMapper with objectMapper.configure(), but not always.
Is this a bad practice? Is there any way to declare it only one or two times?

答案1

得分: 2

我发现实例化 new ObjectMapper() 需要相当长的时间,因此在重复使用之前,您绝对应该重用该对象,而不是每次都创建新的对象。

通常我会像在使用它的类中声明日志记录器一样进行声明(但当然这取决于您的具体要求,是否对您有意义):

private static final ObjectMapper objectMapper = new ObjectMapper();
英文:

I found that the instantiation new ObjectMapper() takes quite long, so you should definitely reuse the object rather than creating it every time before repeated use.

I usually declare it like a logger in the class that'S using it (but of course it depends on your specific requirements whether this makes sense for you):

  private static final ObjectMapper objectMapper = new ObjectMapper();

答案2

得分: 1

The Jackson ObjectMapper有许多不同的配置参数,适用于不同的情况。虽然在操作中跨线程共享实例是可以的,但在需要完全不同的设置时(例如,当您正在使用camel_case命名的REST API,甚至使用YAML或CSV映射器时),需要有很大的差异。

因此,我不建议将ObjectMapper作为bean,就像我不建议将Function<Foo, Bar>String作为bean一样;bean类型不够具体。

相反,ObjectMapper更像是API的一部分,因为诸如“使用camel_case”或“将日期写为ISO 8601字符串”之类的设置不像缓存过期时间,它们反映了合同的固定部分。

我建议创建基于特定用例要求构建新实例的“工厂方法”。这些方法可以存在于公司范围内共享的库中,以强制执行与变量命名、日期/时间处理等相关的一致映射:

public static ObjectMapper companyObjectMapper() {
    // 公司范围内的标准配置
    ...
}

public static ObjectMapper specificRestApiObjectMapper() {
    // 使用Unix纪元进行日期和camel_case
    ...
}
英文:

The Jackson ObjectMapper has lots of different configuration parameters for different circumstances, and while it's okay to share an instance across threads for operations, it is entirely ordinary to need substantially different setups (for example, when you are consuming a REST API that uses camel_case naming, or even using a YAML or CSV mapper).

For this reason, I do not recommend making ObjectMapper a bean any more than I recommend making Function&lt;Foo, Bar&gt; or String beans; the bean type isn't specific enough.

Instead, the ObjectMapper is much more like part of the API, because settings such as "use camel_case" or "write dates as ISO 8601 strings" aren't like cache expiration times, they reflect a fixed part of the contract.

I recommend creating factory methods that will build new instances based on requirements specific to use cases. These methods can live in libraries shared across your company to enforce consistent mapping regarding variable naming, date/time handling, etc.:

public static ObjectMapper companyObjectMapper() {
    // company-wide standard configuration
    ...
}

public static ObjectMapper specificRestApiObjectMapper() {
    // uses Unix epoch for dates and camel_case
    ...

答案3

得分: 1

尽量在可能的情况下重用对象映射器(ObjectMapper),因为它是线程安全的。如果有任何类需要在对象映射器中使用不同的配置,那么只有在这些类需要的情况下才单独实例化一个独立的对象映射器。

我建议不要有相同配置的多个实例。

英文:

Try to reuse objectmapper wherever possible. Because it’s threadsafe. If any classes require different configurations in objectmapper, only then instantiate separate objectmapper for such classes.

I recommend not to have multiple instances of same config.

huangapple
  • 本文由 发表于 2020年8月21日 23:33:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63525809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定