I have been looking for implementing a custom deserializer using LocalTimeDeserializer class of Jackson API.
However, I am getting below error while actually deserializing properties using this class.
com.fasterxml.jackson.databind.JsonMappingException: Class com.dspim.api.common.LocalTimeWithStringDeserializer has no default (no arg) constructor
I am using the custom implementation as below for deserializing inside the bean class.
@JsonProperty @JsonDeserializer(using=LocalTimeWithStringDeserializer.class) private LocalTime packaging_time; //It will hold value for time i.e. 13:24 (01:24 PM).
I have implemented the deserializer class as follows.
package com.testapp.test;
import java.io.IOException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
public class LocalTimeWithStringDeserializer extends com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer{
public LocalTimeWithStringDeserializer(DateTimeFormatter formatter) {
super(formatter);
}
private static final long serialVersionUID = 1L;
@Override
public LocalTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
JsonProcessingException {
return LocalTime.parse(jp.getText());
}
}
The default constructor (with no arguments) in the parent class is private, thus I cannot add a default constructor (with no arguments) to my class implementation as I get compile time error.
Please suggest a solution for this issue.
Please Note: I have two different projects (having dependency on each other added to the classpath) of a client in which I cannot use the built in Jackson deserializer due to dependency version conflicts, that's why I have been compelled to use custom deserializer.
If the parent class has a private no-args constructor, that does not prohibit you from having a no-args constructor yourself:
public class LocalTimeWithStringDeserializer extends com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer{
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
public LocalTimeWithStringDeserializer() {
super(formatter);
}
// ...
}