如何在Payara 5中使用Jackson 2? [英] How to use Jackson 2 in Payara 5?
问题描述
我正在将Jackson 2与Payara 4结合使用,我想在Payara 5中使用 Jackson 2 .
I'm using Jackson 2 with Payara 4 and I would liked to use Jackson 2 in Payara 5.
我还希望避免使用JAX-RS更改批注等.
Using JAX-RS, I also would like to avoid changing annotations and so on...
在Payara 5中,默认的Jsonb提供程序是Yasson. 有禁用它并使用Jackson的想法吗?欢迎所有评论/想法:-)
In Payara 5 the default Jsonb provider is Yasson. Any ideas to disable it and use Jackson instead? All comments/ideas are welcome :-)
注意: Yasson非常有趣,但是处理抽象类或接口的序列化/反序列化要比放置Jackson注释复杂得多. 我目前的理解是,它需要实现JsonbSerializer/Deserializer,但实际上,序列化器/反序列化器仅在字段/方法上可用(为类打开了一个问题,这将非常有帮助). 无论如何,迁移到Yasson意味着将根据需要(对于实体以及当然是集合)实现许多序列化器/反序列化器,但是我想这很困难.
NB: Yasson is very interesting but handle abstract class or interface serialization/deserialization is a little more complex than putting a Jackson annotation. My current understanding is that it requires to implement a JsonbSerializer/Deserializer but actually the serializer/deserializer is only available on field/method (an issue is opened for class, which will be very helpful). Anyway, migrating to Yasson will mean implementing many serializer/deserializer as required (for entities and of course collections) but I guess it's a hard stuff.
推荐答案
您需要将属性jersey.config.jsonFeature
设置为JacksonFeature
,以便不注册默认的JsonB功能.
You need to set the property jersey.config.jsonFeature
to JacksonFeature
so that the default JsonB feature isn't registered.
您可以通过覆盖Application.getProperties()
方法在代码中进行设置,也可以将web.xml中的属性设置为context-param
:
You can set it either in the code by overriding the Application.getProperties()
method, or set the property in web.xml as context-param
:
<context-param>
<param-name>jersey.config.jsonFeature</param-name>
<param-value>JacksonFeature</param-value>
</context-param>
如果您的Jersey Servlet是在web.xml上显式声明的(作为javax.ws.rs.core.Application或作为自定义子类),请在Jersey Application Servlet声明中使用init-param代替:
If your Jersey Servlet is explicitly declared at web.xml (either as javax.ws.rs.core.Application or a as custom subclass), then use init-param instead, within the Jersey Application servlet declaration:
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<init-param>
<param-name>jersey.config.jsonFeature</param-name>
<param-value>JacksonFeature</param-value>
</init-param>
...
</servlet>
您还需要将Jackson依赖项添加到您的应用程序中-但是您可能已经这样做了,因此请忽略此内容.
You also need to add Jackson dependencies into your application - but you have probably done that already so ignore this.
说明:
MOXy功能提供了属性jersey.config.disableMoxyJson
以将其禁用. Payara 5中默认的JsonB功能不提供此类属性,但是如果jersey.config.jsonFeature
属性存在且不是JsonBindingFeature
,则不会自行注册.相同的属性可用于所有Jersey功能,因此将其设置为JacksonFeature
将仅允许注册JacksonFeature.
The MOXy feature provides a property jersey.config.disableMoxyJson
to disable it. The JsonB feature default in Payara 5 doesn't provide such property but will not register itself if jersey.config.jsonFeature
property exists and is not JsonBindingFeature
. The same property works for all Jersey features so setting it to JacksonFeature
will allow only the JacksonFeature to be registered.
这篇关于如何在Payara 5中使用Jackson 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!