对于Jersey 2.17项目,我们还需要JacksonFeature.class吗? [英] Do we still need JacksonFeature.class for Jersey 2.17 projects?

查看:125
本文介绍了对于Jersey 2.17项目,我们还需要JacksonFeature.class吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图知道Jersey 2.17是否还需要JacksonFeature.class。我看不出JacksonFeature.class注册的代码之间的输出有什么区别。

I have been trying to know if JacksonFeature.class is still needed for Jersey 2.17. I can't see any difference between the outputs between codes that JacksonFeature.class is registered or not.

然后,我从codingpedia分叉代码 codingpedia ,删除了JacksonFeature .class,升级到Spring 4.1.2和jersey 2.17,更新了代码并且测试仍然通过。

Then, I forked a code from codingpedia codingpedia, removed JacksonFeature.class, upgraded to Spring 4.1.2 and jersey 2.17, updated the codes and the test still passed.

所以我创建了一个非常简单的Web服务来再次测试< a href =https://github.com/franciszabala/simple-spring-web-service.git =noreferrer> github链接,考虑到删除所有活动部件并仍然有效。那么我们还需要注册JacksonFeature吗?

So I created a very simple web service to test this again github link, having in mind to remove all the moving parts and still worked. So do we still need to register JacksonFeature?

推荐答案

是的我不知道他们为什么使用Jersey 2.9这个教程,但是对于 jersey-media-json-jackson 工件,他们使用的是2.4.1。通常,您应该保持Jersey(相关工件)版本相同。在实际的 Github Project 中,作者将此更改为使用该项目的 $ {jersey.version} (这是2.14),这更有意义。

Yeah I don't know why that tutorial they are using Jersey 2.9, but for the jersey-media-json-jackson artifact, they are using 2.4.1. Generally you should keep the Jersey (related artifact) versions the same. In the actual Github Project, the author changed this to use the project's ${jersey.version} (which is 2.14), which makes more sense.

但要回答您的主要关注点,从版本2.9开始, jersey-media-json-jackson 模块参与 AutoDiscoverable类路径扫描,涉及 Java的服务提供商机制。您可以通过来回切换到此模块的2.8版本和2.9(向上)版本来查看此更改。您将在 META-INF / services 中看到文件 org.glassfish.jersey.internal.spi.Autodiscoverable (列出 JacksonAutoDiscoverable 实现),版本2.9(及更高版本)。这样,除非禁用了可自动发现的功能(可以明确地执行此操作),否则不需要显式配置该功能。

But to answer your main concern, starting from version 2.9 the jersey-media-json-jackson module, takes part in the AutoDiscoverable classpath scanning, which involves Java's Service Provider mechanism. You can see this change by switching back and forth to the 2.8 version and and 2.9 (an upward) version of this module. You will see in the META-INF/services, the file org.glassfish.jersey.internal.spi.Autodiscoverable (which list the JacksonAutoDiscoverable implementation), in version 2.9 (and up). With this, the feature does not need to be explicitly configured, unless the the auto-discoverable feature is disabled (which is possible to explicitly do).

而且为了完整性,当你在类路径上有MOXy,而你没有明确注册Jackson功能时,将使用MOXy,因为MOXy是默认的提供者。即使您可能没有明确依赖MOXy,在使用Glassfish服务器的情况下,它也有MOXy工件,在这种情况下我们可以显式注册Jackson功能,它会自动禁用MOXy,或者我们可以明确禁用MOXy属性 ServerProperties.MOXY_JSON_FEATURE_DISABLE 设置为true

And just for completeness, when you have MOXy on the classpath, and you don't explicitly register the Jackson feature, MOXy will be used, as MOXy is the default provider. Even though you may not have a explicit dependency on MOXy, in situations like the case of using Glassfish server, it has the MOXy artifact, in which case we can either explicitly register the Jackson Feature, which automatically disables MOXy, or we can explicitly disable MOXy with the property ServerProperties.MOXY_JSON_FEATURE_DISABLE set to true

这篇关于对于Jersey 2.17项目,我们还需要JacksonFeature.class吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆