与 Jackson 的 JsonAnySetter 等效的 moxy 是什么? [英] What is the moxy equivalent to Jackson's JsonAnySetter?

查看:23
本文介绍了与 Jackson 的 JsonAnySetter 等效的 moxy 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试迁移到 Jersey 2.0.这让我对 Jackson 感到痛苦,而且文档建议使用 Moxy.

I'm attempting to move to Jersey 2.0. Which is giving me pains with Jackson, and the docs recommend using Moxy.

我让 Moxy 为 get 和 post 调用工作,其中一切都很好地匹配,但是我需要处理可能的未知元素.

I got Moxy working for get and post calls where everything matches nicely however I have a need to deal with possible unknown elements.

// Handle unknown deserialization parameters
@JsonAnySetter
protected void handleUnknown(String key, Object value) {
    if (unknownParameters == null) {
        unknownParameters = new HashMap<>();
    }
    unknownParameters.put(key, value);
}

这在更改为球衣 2.0 之前效果很好,即使我将它留在里面时不会引起任何问题,也永远不会被调用.

This worked well with prior to changing to jersey 2.0 and even though it doesn't cause any problems when I leave it in it never gets called.

我如何在 Jersey 2.0 中实现它?我对 Moxy 或 Jackson 没问题.

How do I implement this in Jersey 2.0? I'm fine with Moxy or Jackson.

我的依赖

'org.codehaus.jackson:jackson-mapper-asl:1.9.2',
'org.eclipse.persistence:org.eclipse.persistence.moxy:2.5.0',
'org.glassfish.jersey.media:jersey-media-moxy:2.0',

这对我的 web.xml 没有影响

This has no effect in my web.xml

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>

推荐答案

您可以使用 jersey-media-json-jackson 模块代替 MOXy 来利用注解(我不确定是否MOXy 支持类似的功能).只是一些注意事项:

You can use jersey-media-json-jackson module instead of MOXy to take advantage of the annotation (I am not sure whether MOXy has support for a similar feature). Just few notes:

  • 从您的 web.xml 中删除 com.sun.jersey.api.json.POJOMappingFeature init 参数 - Jersey 2.x 无法识别此属性(它是 Jersey1.x 特定)
  • 移除org.glassfish.jersey.media:jersey-media-moxy:2.0 依赖
  • 添加 org.glassfish.jersey.media:jersey-media-json-jackson:2.0 依赖项并注册 JacksonFeature 在您的应用程序中(见下文)
  • remove the com.sun.jersey.api.json.POJOMappingFeature init parameter from your web.xml - Jersey 2.x does not recognize this property (it's Jersey 1.x specific)
  • remove org.glassfish.jersey.media:jersey-media-moxy:2.0 dependency
  • add org.glassfish.jersey.media:jersey-media-json-jackson:2.0 dependency and register JacksonFeature in your application (see below)

在这些步骤之后,Jackson 应该处理 Object<->JSON (un)marshalling 并且它应该识别 @JsonAnySetter.

After these steps Jackson should be handling Object<->JSON (un)marshalling and it should recognize @JsonAnySetter.

要在您的应用程序中注册 JacksonFeature,请参阅用户指南中的专用部分 (8.1.4 Jackson):

To register JacksonFeature in your application, see dedicated section in users guide (8.1.4 Jackson):

// Create JAX-RS application.
final Application application = new ResourceConfig()
        .packages("org.glassfish.jersey.examples.jackson")
        .register(MyObjectMapperProvider.class)  // No need to register this provider if no special configuration is required.
        .register(JacksonFeature.class);

这篇关于与 Jackson 的 JsonAnySetter 等效的 moxy 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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