【发布时间】:2018-02-23 12:35:59
【问题描述】:
我正在尝试使用 modelmapper 将我的 Class 与传入的请求进行映射。
Date 似乎无法在 modelmapper 中自动转换。
转换器 org.modelmapper.internal.converter.DateConverter@7595415b 未能将 java.lang.String 转换为 java.util.Date。造成的: org.modelmapper.MappingException:ModelMapper 映射错误:
以上是get的异常。
那么如何单独跳过这个日期字段
目前我的代码看起来像,
ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
MyClass obj=mapper.map(anotherObject,MyClass.class);
我的班级
public class MyClass {
int id;
Date updated_at;
}
anotherObject.toString
{id=6,updated_at=2018-02-23T03:01:12}
更新 2
对这里的误导表示歉意。实际上,我的 anotherObject 不是类对象。我将在下面解释我的确切情况
我的 API 响应
{
"rows": 1,
"last": null,
"results": [
{
"id": "1",
"updated_at;": "2018-01-22T13:00:00",
},
{
"id": "2",
"updated_at;": "2018-01-22T13:00:00",
}
]
}
MysuperClass
public class MysuperClass {
int rows;
String last;
List<Object> results;
}
使用rest模板获取响应正文
ResponseEntity<MysuperClass > apiResponse = restTemplate.exchange(Url, HttpMethod.GET, entity, MysuperClass .class)
MysuperClass anotherObject=apiResponse.getBody();
实际类
ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
for (int index = 0; index < anotherObject.getResults().size(); index++) {
MyClass obj=mapper.map(anotherObject.getResults().get(index),MyClass.class);
}
【问题讨论】:
-
请提供 My class 和 anotherObject 的结构。
-
@Bentaye 更新
-
这可能是一个长镜头,但 JSON 规范要求将字符串包裹在 "" (这是您的日期字段)中,并且由于您启用了 STRICT,它可能会抱怨 JSON 格式无效。
标签: java spring api modelmapper