【问题标题】:How to combine multiple thirdparty POJO into single POJO如何将多个第三方 POJO 组合成单个 POJO
【发布时间】:2023-01-14 22:34:41
【问题描述】:

我们有多个第三方 pojo,我们想将它们组合成单个 pojo,并使用该单个 pojo 使用 jackson 映射到 JSON。

第三方 pojo -

public class ThirdPartyPojo1 {

    private String random1

    //public setters and getters

}
public class ThirdPartyPojo2 {

    private String random2

    //public setters and getters

}

我们想将这些组合起来形成一个单一的 pojo,比如 -

public class ourPojo {
     private String random1;
     private String random2;

     //public setters and getters
}

我们将使用 jackson 将其序列化为 JSON 字符串。我们怎样才能做到这一点?

【问题讨论】:

  • 你会怎样想要处理冲突?
  • @ScottHunter 你能澄清一下,你的意思是什么?
  • 如果他们有任何共同领域怎么办?更糟糕的是,如果它们具有名称相同但类型不同的字段怎么办?
  • 当你写“POJO”时,你的意思是“DTO”吗?
  • @ScottHunter 字段是独一无二的。

标签: java


【解决方案1】:

这是我解决你问题的方法。我不确定它是否有更好的解决方案,因此您可以将其作为参考。我使用 ObjectReader readerForUpdating() 来合并多个 json 源,但这仅限于浅拷贝。您可能需要另一种方法来处理更复杂的对象。这是我的代码:

package jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;

public class Test {

    static ObjectMapper mapper = new ObjectMapper();
    
    public static void main(String[] args) throws Exception {
        // Create instance and convert it to json
        ThirdPartyPojo1 obj1 = new ThirdPartyPojo1();
        obj1.setRandom1("value 1");
        
        // Assume that we receive ThirdPartyPojo1 json 
        String json1 = toJson(obj1);
        
        // Create instance and convert it to json
        ThirdPartyPojo2 obj2 = new ThirdPartyPojo2();
        obj2.setRandom2("value 2");
        
        // Assume that we receive ThirdPartyPojo2 json 
        String json2 = toJson(obj2);
        
        // Suppose the field names of ThirdPartyPojo are corresponding to ourPojo
        // Firstly, use ObjectMapper readValue() to get ThirdPartyPojo1 field i.e. random1 
        ourPojo obj3 = mapper.readValue(json1, ourPojo.class);
        
        // Secondly, use ObjectReader to update ourPojo object
        // Notes that it makes shallow copy only
        ObjectReader updater = mapper.readerForUpdating(obj3);
        // Update ourPojo from ThirdPartyPojo2 field i.e. random2
        obj3 = updater.readValue(json2);
        
        // The result displays merging json into your single POJO
        System.out.println(obj3);
    }
    
    static String toJson(Object obj) throws JsonProcessingException {
        return mapper.writeValueAsString(obj);
    }

}

class ThirdPartyPojo1 {
    private String random1;
    // public setters and getters
}

class ThirdPartyPojo2 {
    private String random2;
    // public setters and getters
}

class ourPojo {
    private String random1;
    private String random2;
    //public setters and getters
}

行家:

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.14.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.14.1</version>
</dependency>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    相关资源
    最近更新 更多