DateConverter 无法将 java.lang.String 转换为 java.util.Date。原因:org.modelmapper.MappingException:ModelMapper 映射错误:

DateConverter failed to convert java.lang.String to java.util.Date. Caused by: org.modelmapper.MappingException: ModelMapper mapping errors:

我正在尝试使用 modelmapper 将我的 Class 映射到传入请求。

似乎日期无法在模型映射器中自动转换。

Converter org.modelmapper.internal.converter.DateConverter@7595415b failed to convert java.lang.String to java.util.Date. Caused by: org.modelmapper.MappingException: ModelMapper mapping errors:

以上是get的异常

那么如何单独跳过这个日期字段

目前我的代码看起来像,

ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
MyClass obj=mapper.map(anotherObject,MyClass.class);

我的Class

public class MyClass {

    int id;

    Date updated_at;

}

anotherObject.toString

{id=6,updated_at=2018-02-23T03:01:12}

更新 2

对误导表示歉意 here.Actually,我的 anotherObject 不是 class 对象。我将在下面解释我的具体情况

我的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();

实际Class

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);

    }

你能试试下面的方法吗:

创建一个新的 class 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());
}

如果您查看 model mapper source code for DateConverter,它似乎只支持 java.sql.Date、java.sql.Time 和 java.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;但是,如果时间很重要,那么使用 Timestamp

(2) 修改 JSON 中的日期格式,使其符合 Timestamp 的预期

话虽如此,另一种选择是与模型映射器团队合作以添加对 java.util.Date 的支持,或者更好的是,更新的 LocalDate 或 LocalDateTime。或者,如果您有兴趣,也可以自己添加支持并提交拉取请求。晚上有空的话可以看看

希望对您有所帮助。

而不是关注 modelmapper alone.I 忘记为我的用例寻找其他库。

我找到了一种使用 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);
    }