我正在尝试使用ModelMapper用传入请求映射我的类。
看来日期无法自动转换ModelMapper。
转换器org.modelmapper.internal.converter.dateconverter@7595415b 无法将java.lang.string转换为java.util.date。由: org.modelmapper.mappingexception:modelMapper映射错误:
以上是获得的例外。
所以如何单独跳过这个日期字段
当前我的代码看起来像
ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
MyClass obj=mapper.map(anotherObject,MyClass.class);
myClass
public class MyClass {
int id;
Date updated_at;
}
enoseobject.tostring
{id=6,updated_at=2018-02-23T03:01:12}
更新2
对此处的误导表示歉意。实际上,我的另一个Objject 不是类对象。我将在下面解释我的确切情况
我的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);
}
如果您查看DateConverter的模型映射源代码,它似乎仅支持Java.sql.date,Java.sql.Time和Java.sql.sql.timestamp类作为目标类型。即便如此,它仅支持每种情况下的源字符串的非常特定的格式。
来自模型映射器DateConverter:
Date dateFor(String source, Class<?> destinationType) {
String sourceString = toString().trim();
if (sourceString.length() == 0)
throw new Errors().errorMapping(source, destinationType).toMappingException();
if (destinationType.equals(java.sql.Date.class)) {
try {
return java.sql.Date.valueOf(source);
} catch (IllegalArgumentException e) {
throw new Errors().addMessage(
"String must be in JDBC format [yyyy-MM-dd] to create a java.sql.Date")
.toMappingException();
}
}
if (destinationType.equals(Time.class)) {
try {
return Time.valueOf(source);
} catch (IllegalArgumentException e) {
throw new Errors().addMessage(
"String must be in JDBC format [HH:mm:ss] to create a java.sql.Time")
.toMappingException();
}
}
if (destinationType.equals(Timestamp.class)) {
try {
return Timestamp.valueOf(source);
} catch (IllegalArgumentException e) {
throw new Errors().addMessage(
"String must be in JDBC format [yyyy-MM-dd HH:mm:ss.fffffffff] "
+ "to create a java.sql.Timestamp").toMappingException();
}
}
throw new Errors().errorMapping(source, destinationType).toMappingException();
}
因此,最简单的修复是:
(1(更改myclass以使用java.sql.date而不是java.util.date;但是,如果时间很重要,请使用时间戳
(2(修改JSON中日期的格式可以接受时间戳期望
话虽如此,另一种选择是与模型映射团队合作,以增加对Java.util.date的支持,或者更好的是更新的Local Date或LocalDateTime。或者,如果您有兴趣,也可以自己添加支持并提交拉动请求。如果我有时间,我可能会看看今天晚上那个晚上。
希望这会有所帮助。
您可以尝试以下操作:
创建一个新类Result
,以将JSON results
数组的元素映射到。
class Result {
int id;
String updated_at;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
// setter should take a string (as it is in the JSON)
public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}
// the getter should return a Date to map with MyClass
public Date getUpdated_at() {
Date d = null;
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
d = format.parse(updated_at);
} catch(ParseException e) {
e.printStackTrace();
}
return d;
}
}
更改您的MysuperClass
并使results
列表
List<Result> results;
然后尝试以下代码:
ResponseEntity<MysuperClass > apiResponse = restTemplate.exchange(Url, HttpMethod.GET, entity, MysuperClass .class)
MysuperClass anotherObject = apiResponse.getBody(); // I hope that works fine
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
for(Result result : anotherObject.getResults()) {
MyClass obj = modelMapper.map(result, MyClass.class);
System.out.println(obj.getId());
System.out.println(obj.getUpdated_at());
}
,而不是单独焦点模型mapper。
我使用ObjectMapper(org.codehaus.jackson.map.objectmapper(找到了一种简单的方法
List<<Result> results=(List<Result>)(Object)anotherObject.getResults();
for(Object myObject: results){
ObjectMapper objectMapper=new ObjectMapper();
Appointment appointment= objectMapper.convertValue(myObject, MyClass.class);
}