Cannot construct instance of `class name` (although at least on Creator exists)
Hi you need to write custom deserializer as it not able to parse String (fromDate and toDate) to Date
{ "fromDate":"2019-03-09", "toDate":"2019-03-10" }
this link has a tutorial to get started with custom deserializer https://www.baeldung.com/jackson-deserialization
Deserializer could be written like this.
public class CustomDateDeserializer extends StdDeserializer<Date> {
private static SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
public CustomDateDeserializer() {
this(null);
}
public CustomDateDeserializer(Class<?> vc) {
super(vc);
}
@Override
public Date deserialize(JsonParser jsonparser, DeserializationContext context) throws IOException {
String date = jsonparser.getText();
try {
return formatter.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}}
You can register the deserializer at Class itself like this.
@JsonDeserialize(using = ItemDeserializer.class)
public class Item { ...}
Or either you can register custom deserializer manually like this
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Item.class, new ItemDeserializer());
mapper.registerModule(module);
You need a constructor with all parameters:
public SampleRequest(String fromDate, String toDate) {
this.fromDate = fromDate;
this.toDate = toDate;
}
Or using @AllArgsConstructor
or @Data
from lombok.
In my case i was missing the No Args contructor
@Data
@AllArgsConstructor
@NoArgsConstructor
for those who are no using Lombok do add no args constructor in the mapping pojo
public ClassA() {
super();
// TODO Auto-generated constructor stub
}
also dont forget to add the Bean of Restemplate in main file if you are using the same