实体有字段类型为Date类型,页面传过去的为string类型是出现类型不匹配。
解决办法:
在实体字段的setter上加上注解:
@JsonDeserialize(using = CustomJsonDateDeserializer.class)
CustomJsonDateDeserializer类代码:
public class CustomJsonDateSerializer extends JsonDeserializer<Date>{
@Override
public Date deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
// TODO Auto-generated method stub
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = p.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}