Unstable behavior with Jackson Json and @JsonAnySetter

十年热恋 提交于 2020-03-19 06:33:09

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/59028677/unstable-behavior-with-jackson-json-and-jsonanysetter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!