Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unstable behavior with Jackson Json and @JsonAnySetter

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.

like image 604
Ismail Avatar asked Nov 25 '19 09:11

Ismail


1 Answers

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.

like image 71
Ismail Avatar answered Oct 16 '22 23:10

Ismail