Unstable behavior with Jackson Json and @JsonAnySetter
Asked Answered
I

1

6

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.

Isotope answered 25/11, 2019 at 9:29 Comment(2)
looks like different jackson versions on the class path.Waterhouse
When problems like this happen probably the reason is a server and how it loads classes. Try to check on every VM that Classloading is set in the same way and Jackson is loaded from your app not from preinstalled module.Counterrevolution
I
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.

Isotope answered 28/11, 2019 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.