什么是moxy相当于Jackson的JsonAnySetter? [英] What is the moxy equivalent to Jackson's JsonAnySetter?

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

问题描述

我正试图转移到泽西岛2.0。这给了我杰克逊的痛苦,而且文档建议使用Moxy。

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

我让Moxy工作,获取和发布所有匹配良好的电话然而我需要处理可能未知的元素。

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',

这对我的网站没有影响.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:


  • 删除 com.sun.jersey.api.json.POJOMappingFeature 来自 web.xml 的init参数 - Jersey 2.x无法识别此属性(特定于Jersey 1.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)

在这些步骤之后杰克逊应该处理对象< - > JSON(联合国)编组,它应该识别 @JsonAnySetter

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

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

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);

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

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