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 ignorable
although 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.
VM
that Classloading is set in the same way andJackson
is loaded from your app not from preinstalled module. – Counterrevolution