当前,我收到来自API端点的以下格式的请求。
{"amount":"122.96","timestamp":"2019-05-28T12:19:54.702Z"}
我想尝试将时间戳转换为以下资源中的Calendar对象。
public class resource {
@JsonProperty
private BigDecimal amount;
@JsonProperty
private Calendar timeStamp;
}
但是,由于以下原因,端点无法映射:
java.lang.Error: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.api.Resource]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.api.Resource` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)at [Source: (PushbackInputStream); line: 1, column: 2]
我尝试使用附加的自定义反序列化器在下面添加@JsonFormat和@JsonDeserializer
CustomDeserializer.class
public class CalendarDeserializer extends StdDeserializer<Calendar> {
protected CalendarDeserializer() {
super(Calendar.class);
}
public Calendar deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c.setTimeInMillis(jp.readValueAs(Long.class));
return c;
}
}
我希望能够将时间戳正确地映射到日历对象