lundi 2 mai 2022

How to change class primitive fields to respective wrapper fields dynamically?

I have a class from third party jar which have primitive fields inside it. For example:

public class A {

private int id;

private int score;

//Getter And Setters below
}

I have another class which have all fields of A class and have more fields as well and can be partially updated. For example

public class B {

private Integer id;

private Integer score;

private Integer age;

//Getter and Setters below
}

B class is in my project and can be modified. Partial update is allowed in B class for example

B b = new B();
b.setAge(32);

Now using ModelMapper class I am doing the mapping of both the classes.

A object = new ModelMapper().map(b, A.class);

ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(object, JsonNode.class);

As A class have primitive data type fields hence after mapping those fields get initialized with default value.

Current Outcome Expected Outcome
{"id" : 0, "score" : 0 ,"age" 32} {"age": 32}

Is there a way using which we can convert the A class primitive data types to wrapper classes like int to Integer so that instead of default value it get initialized with NULL value and later on can be ignore in JSON converter?

Or is there any way using which we can achieve this using ModelMapper.





Aucun commentaire:

Enregistrer un commentaire