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<>();
Related
I am very new to java and I hope my question is not too stupid and has enough info for you guys to help me out.
I have a list of jsonNodes, each of them is in the following format:
{"field1":value1, "field2":value2, "field3":value3, "notneeded1":value4, "notneeded2":value5}
I am currently using a class like the following and converting it to list
#JsonIgnoreProperties(ignoreUnknown = true)
class customClass:
String field1;
String field2;
String field3;
Using TypeReference to convert the list of jsonNode to list of this class...
What I want to do is add a few of these fields within an another field
{"parentfield":{"field1":value1, "field2":value2}, "field3":value3}
How do I do this using this class?
AFIU you want this:
#JsonIgnoreProperties(ignoreUnknown = true)
class CustomClass {
public String field1;
public String field2;
public String field3;
public CustomClass2 parentfield;
}
#JsonIgnoreProperties(ignoreUnknown = true)
class CustomClass2 {
public String field1;
public String field2;
}
Then depends on your code, to set the values of the fields in an object of CustomClass2 with fields from an object of CustomClass.
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
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
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 a class:
public class MyCustomObject {
private String field1;
private String field2;
}
And I've created array of MyCustomObject:
MyCustomObject[] array = new MyCustomObject[]{new MyCustomObject()};
My goal is to verify elements of this array using hamcrest matchers. I've tried the following approach:
assertThat(array, allOf(hasItemInArray(hasProperty("field1", equalTo("value1")))), hasItemInArray(hasProperty("field2", equalTo("value2")))));
But unfortunatly it does not work.
In which way the array of custom objects can be verified?
I would change your array to an ArrayList just for testing purposes:
List<MyCustomObject> customObjects = Arrays.asList(array);
And then assert with the Hamcrest hasItems Matcher if the expected items are present in the list:
assertThat(customObjects, hasItems(myCustomObject1, myCustomObject2));
Have you tried adding getters to your class? That did the trick for me.
public class MyCustomObject {
private String field1;
private String field2;
public String getField1() {
return field1;
}
public String getField2() {
return field2;
}
}