杰克逊,反序列化嵌套对象

安德烈Yaskulsky:

我有应该由弹簧反序列化以下请求结构:

{
  "firstname": "Iev",
  "lastname": "Ivano",
  "createdProfile": {
    "from": "2019-07-03T15:41:52.854+03:00",
    "to": "2019-07-05T15:41:52.854+03:00"
  }
}

我的DTO的样子

class UserDto {

    private String firstname;

    private String lastname;

    @JsonProperty("createdProfile.from")
    private ZonedDateTime createdProfileFrom;

    @JsonProperty("createdProfile.to")
    private ZonedDateTime createdProfileTo;
    //gets 
    //sets
}

所以,我想春天来填充

 ZonedDateTime createdProfileFrom and ZonedDateTime createdProfileTo

 "createdProfile": {
    "from": "2019-07-03T15:41:52.854+03:00",
    "to": "2019-07-05T15:41:52.854+03:00"
  }

考虑到我注释与对应领域

  @JsonProperty("createdProfile.from") and @JsonProperty("createdProfile.to") 

但出于某种原因春天不想来填充这些字段。

会明白任何帮助,

谢谢

Taygetos山:

你UserDto对象应该是这样的:

public class UserDto {

  private String firstname;
  private String lastname;
  private ZonedDateTime createdProfileFrom;
  private ZonedDateTime createdProfileTo;

  @JsonProperty("createdProfile")
  private void unpackNested(Map<String,Object> elements) {
    this.createdProfileFrom = ZonedDateTime.parse((String)elements.get("from"), DateTimeFormatter.ISO_DATE_TIME);
    this.createdProfileTo = ZonedDateTime.parse((String)elements.get("to"), DateTimeFormatter.ISO_DATE_TIME);
  }

   //getters + setters
}

反序列化期间你访问createdProfile元件与所述注释和在该方法本身你读的嵌套元素。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章