杰克逊注释被忽略 [英] jackson annotations being ignored

查看:31
本文介绍了杰克逊注释被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Jackson 注释来重命名序列化期间生成的一些 json 标签.注释都编译得很好,当我运行时,除了所有 Jackson 注释都被完全忽略之外,Jackson 序列化工作正常.甚至像@JsonIgnore 或@JsonProperty 这样的基本函数对 json 响应也没有影响.我在构建路径中的库是:

I'm trying to use Jackson annotations to re-name some of the json labels produced during serialization. The annotations all compile fine, and when I run, the Jackson serialization works except all Jackson annotations are completely ignored. Even the basic ones like @JsonIgnore or @JsonProperty have no effect on the json response. The libraries I have in my build path are:

jsr311-qpi-1.1.1.jar
jackson-[core|databind|annotations]-2.2.0.jar

我在 Eclipse 中运行 jetty 外部程序,外部程序设置为:

I'm running within Eclipse running the jetty external program with the External program setup as:

Location: .../apache-maven-2.2.1/bin/mvnDebug.bat
working Directory: ${workspace_loc:/ohma-rest-svr}
Arguments: jetty:run

将远程 Java 应用程序配置设置为:

with the Remote Java Application configuration set as:

Host: localhost
Port: 8000

由于没有错误消息可供使用,我有点不知所措.任何想法将不胜感激.

With no error messages to work from, I'm at a bit of a loss of things to try. Any ideas would be appreciated.

这是我需要序列化的类的一些代码示例:

Here's a bit of code sample from a class I need to serialize:

@XmlRootElement(name="ads-parameter")
public class DefineParameterResponse {

    private Date _createdAt = new Date();

    @JsonProperty("created-at")
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd,HH:00", timezone="CET")
    @XmlElement
    public String getCreatedAt() {
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(_createdAt);
    }

    @JsonProperty("created-at")
    public void setCreatedAt(Date createdAt) {
        this._createdAt = createdAt;
    }


    private String _dataTitle1 = "Default Title1";
    @XmlElement
    @JsonProperty("data-title-1")
    public String getDataTitle1() {
        return _dataTitle1;
    }

    @JsonProperty("data-title-1")
    public void setDataTitle1(String dataTitle1) {
        this._dataTitle1 = dataTitle1;
    }


    @XmlElement
    @JsonProperty("data-title-2")
    public String getDataTitle2() {
        return _dataTitle2;
    }

    @JsonProperty("data-title-2")
    public void setDataTitle2(String dataTitle2) {
        this._dataTitle2 = dataTitle2;
    }

推荐答案

一个相对常见的原因是试图使用错误"的注释集:Jackson 1.x 和 Jackson 2.x 注释存在于不同的 Java 包中,而 databind必须匹配主要版本.这种设计的好处是允许 1.x 和 2.x 版本并行使用,不会发生类加载冲突;但缺点是你必须确保你有匹配的版本.

One relatively common reason is trying to use "wrong" set of annotations: Jackson 1.x and Jackson 2.x annotations live in different Java packages, and databind has to match major version. This design has the benefit of allowing 1.x and 2.x versions to be used side by side, without class loading conflicts; but downside that you have to make sure that you have matching versions.

最大的问题是框架的使用:许多 JAX-RS 实现(如 Jersey)在默认情况下仍然使用 Jackson 1.x.所以我猜你可能间接使用 Jackson 1.x,但添加了 Jackson 2.x 注释.如果是这样,您需要使用 1.x 注释(org.codehaus.jackson 下的注释).

Biggest problem is the use by frameworks: many JAX-RS implementations (like Jersey) still use Jackson 1.x by default. So I am guessing you might be using Jackson 1.x indirectly, but adding Jackson 2.x annotations. If so, you need to use 1.x annotations (ones under org.codehaus.jackson) instead.

这篇关于杰克逊注释被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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