Java:对象进行对象的列表

发布于 2025-01-21 07:18:03 字数 2133 浏览 1 评论 0原文

我有这个json :(作为map< string,object>

{
  "id": 1000,
  "lab": [
    "LAB1",
    "LAB2",
    "LAB3"
  ],
  "name": "TEST",
  "ref": {
    "id": 1000,
    "code": "REFCODE",
    "description": "REF DESC"
  },
  "employee": {
    "id": 1000,
    "name": "Emp1000",
    "tin": null,
    "active": true
  },
  "contacts": [
    {
      "id": 1000,
      "name": "Contact 1",
      "emailAddress": "[email protected]",
      "active": true,
      "positions": [
        {
          "position": {
            "id": 1000,
            "code": "POS",
            "description": "POS DESC"
          }
        }
      ]
    }
  ],
  "status": "NEW"
}

这是我的DTO和ContactDto:

public class DTO {
    private Long id;
    ...

    @JsonProperty("contacts")
    private List<ContactDTO> contacts; 

}

@Builder
public class ContactDTO implements Serializable {    
    private Long id;
    private String name;
    private String emailAddress;
    private Boolean active;
    @NotEmpty
    private List<ContactPositionDTO> positions;
}

这是我的服务类,带有对象映射器和过程方法,该方法接受JSON MAP:

private ObjectMapper objectMapper() {
    var objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JavaTimeModule());
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    // objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
    return objectMapper;
}

public void process(final Map<String, Object> map) {
    objectMapper().convertValue(map, DTO.class);
}

但是,我是获取java.lang.lang.illegalargumentException:无法应对类型的java.util.arraylist的值

,如果我添加deserializationfeature.accept_single_value_value_as_ars_array 我得到了不同的错误:

java。 lang.illegalgumentException:无法构造contactdto的实例(尽管至少存在一个创建者):没有字符串argument constructor/factory方法可以从字符串值中进行估算('[{iD = 1000,name = contact 1 ,, 1 ,, ..... << /代码>

I have this JSON: (passed as Map<String, Object>)

{
  "id": 1000,
  "lab": [
    "LAB1",
    "LAB2",
    "LAB3"
  ],
  "name": "TEST",
  "ref": {
    "id": 1000,
    "code": "REFCODE",
    "description": "REF DESC"
  },
  "employee": {
    "id": 1000,
    "name": "Emp1000",
    "tin": null,
    "active": true
  },
  "contacts": [
    {
      "id": 1000,
      "name": "Contact 1",
      "emailAddress": "[email protected]",
      "active": true,
      "positions": [
        {
          "position": {
            "id": 1000,
            "code": "POS",
            "description": "POS DESC"
          }
        }
      ]
    }
  ],
  "status": "NEW"
}

This is my DTO and ContactDTO:

public class DTO {
    private Long id;
    ...

    @JsonProperty("contacts")
    private List<ContactDTO> contacts; 

}

@Builder
public class ContactDTO implements Serializable {    
    private Long id;
    private String name;
    private String emailAddress;
    private Boolean active;
    @NotEmpty
    private List<ContactPositionDTO> positions;
}

Here is my service class with object mapper and process method which accepts the JSON map:

private ObjectMapper objectMapper() {
    var objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JavaTimeModule());
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    // objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
    return objectMapper;
}

public void process(final Map<String, Object> map) {
    objectMapper().convertValue(map, DTO.class);
}

However, I am getting java.lang.IllegalArgumentException: Cannot deserialize value of type java.util.ArrayList

And if I add DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY I am getting a different error:

java.lang.IllegalArgumentException: Cannot construct instance of ContactDTO (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('[{id=1000, name=Contact 1, .....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱情眠于流年 2025-01-28 07:18:03

您有两个替代选项来修复contactdto类:

  • 添加no-arguments constructor
    公共contactdto(){}
    上课。要修复当时即将发生的编译器错误
    您将需要删除@builder注释。
  • 保留@builder注释
    并将@jacksonized注释到类。
    这将配置生成的构建器以合作
    杰克逊的挑战。
    有关更多详细信息,请参见 lombok关于@jacksonized 的文档。

You have two alternative options to fix your ContactDTO class:

  • Add a no-arguments constructor
    public ContactDTO() { }
    to the class. To fix the then upcoming compiler error
    you will need to remove the @Builder annotation.
  • Keep the @Builder annotation
    and add the @Jacksonized annotation to the class.
    This will configure the generated builder to cooperate
    with Jackson's deserialization.
    For more details see Lombok's documentation about @Jacksonized.
她说她爱他 2025-01-28 07:18:03

为了进行挑选,您需要一个no arg构造函数并使用@builder您需要一个All arg构造函数。
因此,您需要添加两者。
下面的示例应起作用。我刚刚添加了@getter注释以避免使用@jsonproperty

@Getter
public static class DTO {

    private Long id;

    @JsonProperty("contacts")
    private List<ContactDTO> contacts;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class ContactDTO implements Serializable {

    private Long id;

    private String name;

    private String emailAddress;

    private Boolean active;

    private List<ContactPositionDTO> positions;
}

@Getter
public static class ContactPositionDTO {

    private Position position;

    @JsonProperty("effectiveStartDate")
    private List<Integer> date;

    @Getter
    static class Position {

        private Integer id;

        private String code;

        private String description;
    }
}

nb:您还可以使用@jacksonized注释,而不是@noargsconsstructorr 和@allargsconstructor

To deserialize you need a No arg constructor and to use @Builder you need an all arg constructor.
So you need tu add both.
The example below should work. I just added @Getter annotation to avoid using @JsonProperty

@Getter
public static class DTO {

    private Long id;

    @JsonProperty("contacts")
    private List<ContactDTO> contacts;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class ContactDTO implements Serializable {

    private Long id;

    private String name;

    private String emailAddress;

    private Boolean active;

    private List<ContactPositionDTO> positions;
}

@Getter
public static class ContactPositionDTO {

    private Position position;

    @JsonProperty("effectiveStartDate")
    private List<Integer> date;

    @Getter
    static class Position {

        private Integer id;

        private String code;

        private String description;
    }
}

NB: you can also use @Jacksonized annotation instead of @NoArgsConstructor and @AllArgsConstructor

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文