杰克逊·杰森(Jackson Json)和@JsonAnySetter的行为不稳定 [英] Unstable behavior with Jackson Json and @JsonAnySetter

查看:113
本文介绍了杰克逊·杰森(Jackson Json)和@JsonAnySetter的行为不稳定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jackson进行Json序列化和反序列化的Java Web项目. 我正在将Jetty用作Web服务器 我正在尝试在构建时反序列化生成的类:

/**
 *Generated class at compile time
**/    
@JsonInclude(NON_NULL)
    public class SamplePayloadContent extends AbstractSamplePayload {

        @NotNull
        @JsonProperty(value = "sampleProperty", required = true)
        private String sampleProperty;

       ...
    }

我正在使用AbstractSamplePayload将属性添加到生成的类AbstractSamplePayload中:

public abstract class AbstractSamplePayload implements Serializable {

    protected final static transient Logger logger = LoggerFactory.getInstance(AbstractSamplePayload.class.getClass());

    /**
     *
     */
    private static final long serialVersionUID = 8422742687709239202L;


    @JsonAnySetter
    public void handleUnknown(String key, Object value) {

        logger.warn(new LogMetadata(ELogIds.ABST_SAMPLE_PAYLOAD, "Missing setter for key " + key + " value " + value));
    }
}

因此,使用@JsonAnySetterhandleUnknown方法,我试图忽略其他属性.

之后,我尝试使用ObjectMapper反序列化Json格式的字符串:

public SamplePayloadContent buildContent(String jsonPayload) throws IOException{
     ObjectMapper objectMapper=new ObjectMapper();

     return objectMapper.readValue(jsonPayload, SamplePayloadContent.class);

}

构建项目后,通过反序列化,一切在运行时都可以正常工作,其他属性将被正确忽略.

将Web存档部署到不同的VM中后,其中一些,当尝试使用先前的方法构建对象时,尽管我们具有@JsonAnySetter,但应用程序将抛出UnrecognizedPropertyException: Unrecognized field "sampleProperty" , not marked as ignorable.

在通过添加一些包含SamplePayloadContent类的方法列表的日志来研究问题之后,我发现存在问题的VM无法识别扩展方法handleUnknown.

这里的怪异行为是,所有VM都具有相同版本的Java和Jetty,以及相同的OS,但我们与其中的一些交互性很差..

预先感谢您的帮助.

解决方案

正如我所说,我们正在许多不同的VM上运行该应用程序,而我们刚刚在其中一些中遇到了问题. 我们在类路径中存在的同一个包中找到了另一个具有相同名称SamplePayloadContent的生成的类,但是该类不继承自AbstractSamplePayload.第二类来自已部署应用程序使用的依赖项.

因此,在某些VM中,Jetty考虑了错误的类,因此我们得到并抛出异常,而在另一些VM中,Jetty考虑了正确的类,因此我们得到了预期的行为.

I am working on a Java web project using Jackson for Json serialization and deserializtion. I am using Jetty as a web server I am trying to deserialize a generated class at build time:

/**
 *Generated class at compile time
**/    
@JsonInclude(NON_NULL)
    public class SamplePayloadContent extends AbstractSamplePayload {

        @NotNull
        @JsonProperty(value = "sampleProperty", required = true)
        private String sampleProperty;

       ...
    }

I am using AbstractSamplePayload to add propeties to the generated class, AbstractSamplePayload:

public abstract class AbstractSamplePayload implements Serializable {

    protected final static transient Logger logger = LoggerFactory.getInstance(AbstractSamplePayload.class.getClass());

    /**
     *
     */
    private static final long serialVersionUID = 8422742687709239202L;


    @JsonAnySetter
    public void handleUnknown(String key, Object value) {

        logger.warn(new LogMetadata(ELogIds.ABST_SAMPLE_PAYLOAD, "Missing setter for key " + key + " value " + value));
    }
}

So with the @JsonAnySetter and handleUnknown method, I am trying to ignore additional properties.

After, I am trying to deserialize a Json format string with ObjectMapper:

public SamplePayloadContent buildContent(String jsonPayload) throws IOException{
     ObjectMapper objectMapper=new ObjectMapper();

     return objectMapper.readValue(jsonPayload, SamplePayloadContent.class);

}

After building the project, everything is working fine at runtime with the deserialization, at the additional properties are ignored properly.

After deploying the Web archive in different VMs, some of them, when try to build our object with the previous method, the application throws UnrecognizedPropertyException: Unrecognized field "sampleProperty" , not marked as ignorablealthough we have the @JsonAnySetter.

After digging in the issue by adding some logs that contains the list of methods of the SamplePayloadContent class, I figured out that the VMs that hold the problem, do not recognize the extended method handleUnknown.

The weird behavior here is all the VMs have the same version of Java and Jetty and the same OS, but we are getting bad interaction with some of them.

Thanks in advance for your help.

解决方案

As I said, we are running the application on many different VMs, and we have just encountered the problem in some of them. We have found another generated class with same name SamplePayloadContent and in the same package that exists in the classpath but this class doesn't inherit from AbstractSamplePayload. The second class come from a dependency used by the deployed app.

So, in some VMs Jetty consider the wrong class so we get and exception, and in the other VMs Jetty consider the right class so we get the expected behavior.

这篇关于杰克逊·杰森(Jackson Json)和@JsonAnySetter的行为不稳定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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