超类值的 Jackson JSON 映射

发布于 2024-11-19 21:24:07 字数 554 浏览 1 评论 0原文

我在 Spring 应用程序中使用 Jackson 1.8.3 将 Java 对象映射到 JSON。

我的一个 Java 类 (Child) 扩展了一个属于库一部分的超类 (Parent),因此我无法修改它。 (特别是我无法添加注释。)

我使用 @JsonAutoDetect(JsonMethod.NONE) 因为我只需要对象中的一小组字段,而是使用 @JsonProperty

class Parent {
  public long getId(){...};
  ...
}

@JsonAutoDetect(JsonMethod.NONE)
class Child extends Parent {

    @JsonProperty
    private String title;
}

但是我需要的字段之一是来自超类的字段id,但我不知道如何告诉Jackson注意这个字段,而不修改父类(因为我无法修改)。

I am using Jackson 1.8.3 in a Spring application to map Java Objects to JSON.

One of my Java Class (Child) extends an super class (Parent) that is part of an Libary, so I am not able to modify it. (Especially I am not able to add annotations.)

I am using @JsonAutoDetect(JsonMethod.NONE) because I need only a small set of fields from the object, instead I am using @JsonProperty.

class Parent {
  public long getId(){...};
  ...
}

@JsonAutoDetect(JsonMethod.NONE)
class Child extends Parent {

    @JsonProperty
    private String title;
}

But one of the fields I need is an field id from the superclass, but I don't know how to tell Jackson to pay attention to this field, without modifying the parent class (because I can not modify it).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恬淡成诗 2024-11-26 21:24:07

如果将注释放在 getter 上(而不是直接放在字段上),则可以重写 getId() 方法(如果它在超类中不是最终方法)并向其添加注释。

class Parent {
  public long getId(){...};
  ...
}

@JsonAutoDetect(JsonMethod.NONE)
class Child extends Parent {

    private String title;

    @JsonProperty
    public String getTitle() {...}

    @Override
    @JsonProperty
    public long getId() {
        return super.getId();
    }
}

If you put the annotations on the getters (instead directly on the fields), you can override the getId() method (if it's not final in the superclass) and add an annotation to it.

class Parent {
  public long getId(){...};
  ...
}

@JsonAutoDetect(JsonMethod.NONE)
class Child extends Parent {

    private String title;

    @JsonProperty
    public String getTitle() {...}

    @Override
    @JsonProperty
    public long getId() {
        return super.getId();
    }
}
水中月 2024-11-26 21:24:07

我无法添加注释

您可以使用混合添加注释。有关详细信息,请参阅 http://wiki.fasterxml.com/JacksonMixInAnnotations

根据类字段/方法结构的其余部分,另一种可能有效的方法是通过 ObjectMapper 配置字段/方法的可见性访问。下面一行演示了如何进行这样的配置,不一定是您需要的具体配置。

mapper.setVisibilityChecker(mapper.getVisibilityChecker().withFieldVisibility(Visibility.ANY));

我使用@JsonAutoDetect(JsonMethod.NONE),因为我只需要对象中的一小部分字段,而我使用@JsonProperty

我应该意识到这是可能的。我必须更新我的博客文章。谢谢。

I am not able to add annotations

You can add annotations using mix-ins. See http://wiki.fasterxml.com/JacksonMixInAnnotations for details.

Depending on what the rest of the class feild/method structures are, another approach that might work would be to configure visibility access of fields/methods through ObjectMapper. The following line demonstrates how to make such a configuration, not necessarily the specific configuration you need.

mapper.setVisibilityChecker(mapper.getVisibilityChecker().withFieldVisibility(Visibility.ANY));

I am using @JsonAutoDetect(JsonMethod.NONE) because I need only a small set of fields from the object, instead I am using @JsonProperty

I should have realized that was possible. I have to update my blog post. Thank you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文