I have four class
public class A {
private String field1;
private String field2;
private B b;
// setter and getter
}
public class B {
private String fieldB1;
private String fieldB2;
// setter and getter
}
public class ADto {
private String field1;
private String field2;
private BDto b;
// setter and getter
}
public class BDto {
private String fieldB1;
private String fieldB2;
// setter and getter
}
I want to use modelmapper to populate dto objects
ModelMapper modelMapper = new ModelMapper();
modelMapper.typeMap(A.class, ADto.class, "LAZY")
.addMappings(mapper -> mapper.skip(ADto::setB))
.addMappings(mapper -> mapper.skip(ADto::setField2));
modelMapper.map(a, ADto.class, "LAZY");
I use typeMapName to be able to have more mappers
The mapper correctly skips field2 but instead ignores the skip for B, what am I wrong?
Thanks
Related
I have a class with a nested list and I want to map this object into list of flat objects using ModelMapper.
public class A {
private String str;
private String str2;
private List<B> blist;
// Setters and getters
}
public class B {
private String str3;
private String str4;
// Setters and getters
}
public class C {
private String str;
private String str2;
private String str3;
private String str4;
// Setters and getters
}
I want to convert an object of class A into a list of objects of class C
public class B {
private String name;
private String value;
//Setters and Get
}
public class C {
private String name;
private String value;
//Setters and Get Methods
}
public class D {
private String name;
private String value;
//Setters and Get
}
public class A {
private B b;
private C c;
private D d;
// Setters and Get
}
public class Example{
List<A> a = new Array List<A>();
//Lets assume a will contain objects of class B, C and D
a .sort( Comparator.comparing(A::getB().getName).thenComparing(A::getC().getName));
}
Sort field from one pojo , then sort field by next pojo.
Need to understand how to sort in this situation. Can we use
Comparator.comparing ()in this case?
You can't use method refences like that, but you could just use lambda expressions:
a.sort(Comparator.comparing((A x) -> x.getB().getName())
.thenComparing(x -> x.getC().getName()));
I have a very basic OOP question. I have two classes as follows
public class Class1 {
private String field1;
private String field2;
private List<String> field3;
// constructors
}
public class Class2 {
private String field1;
private String field2;
private List<Object1> field3;
// constructors
}
The only reason i had to create class2 was because the List in class1 can either be a List of Strings or a List of Object2. Is there a way to et rid of Class2, and have field3 behave as a List of String and also as a List of Object2.
I know i can do something like
public class Class1 {
private String field1;
private String field2;
private List<String> field3;
private List<Object1> field4;
// constructors which are initializing null to either field 3 or field 4
}
But the above doesnt seem clean, as an object instantiated with either list should not have any reference in it of the other List.
Use generics:
public class Class1<TField3> {
private String field1;
private String field2;
private List<TField3> field3;
// constructors
}
Class1<String> stringObj = new Class1<>();
I have two java beans as below:
public class Class1 {
private String field1;
private Object field2;
//getter and setter
}
public class Class2 {
private Map<String,Object> field;
//getter and setter
}
When the object gets serialized to Json, it looks like this:
Class1: When field2 is null
{
field1:"value"
}
Class2: when value of map is null
{
field:{"key":null}
}
My question is what is the difference between the two? why for Class1 it didn't include null field in json? How do I include null field in json for Class1? I have tried the following but did't work:
#JsonInclude(JsonInclude.Include.ALWAYS)
public class Class1 {
private String field1;
private Object field2;
//getter and setter
}
and even tried on field level:
public class Class1 {
private String field1;
#JsonInclude(JsonInclude.Include.ALWAYS)
private Object field2;
//getter and setter
}
I am using Jersey.
The following example is with jackson:
ObjectMapper mapper = new ObjectMapper();
Class1 class1 = new Main().new Class1();
System.out.println(mapper.writeValueAsString(class1));
and the output is:
{"field1":null,"field2":null}
I have below json string :-
{"name":"Test","sortlist":[],"filterlist":[{"fieldname":"regions_id","operator":"equals","value":{"id":1,"code":"HIGH","description":"HIGH Region","comment":"High Region","active":true}}]}
and Java class as below :-
#JsonSerialize
#JsonDeserialize
public class ItemFilter implements Serializable {
private String name;
private List<FieldFilter> filterlist = new ArrayList<FieldFilter>();
}
public class FieldFilter implements Serializable {
private static final long serialVersionUID = 1L;
private String fieldname;
private String operator;
private Object value;
}
and my convert method as below :-
public static ItemFilter convertItemFilter(String item) {
ObjectMapper mapper = new ObjectMapper();
try {
ItemFilter itemFilter = mapper.readValue(item, new TypeReference<ItemFilter>(){});
return itemFilter;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
ItemFilter domain is getting converted correctly but in private Object value; field i am getting LinkedHashMap i want to get an simple object and later i will type cast it.
Can someone please guide me how to escape LinkedHashMap and get an simple Java Object in variable?
i cant use hard coding Object type because its a generic pojo which can have any object type. hard coding will make this pojo very bigger and frontend also need to change for it. So that why i have used Object as data type.
The following class structure should return the JSON to "YourObject"
public class YourObject{
private String name;
private List<String> sortList;
private List<Filter> filterList;
public static class Filter{
private String fieldname;
private String operator;
private Value value;
}
public static class Value{
private Integer id;
private String code;
private String description;
private String comment;
private Boolean active;
}
}
Then use the following to read it into the object:
YourObject itemFilter = mapper.readValue(item, YourObject.class);