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

查看:17
本文介绍了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 服务来再次测试它 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项目中,作者将这个改为使用项目的${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的Service Provider机制.您可以通过来回切换到此模块的 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 工件,在这种情况下,我们可以显式注册自动禁用 MOXy 的 Jackson 功能,或者我们可以显式禁用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天全站免登陆