I have an end point which returns large chunk of data and I want to remove some part of it.
For example:
Class A
public class A{
private String id;
private Date createOn;
private String processed;
}
Class B
public class B extends MongoDBObject{
private String id;
private Date createOn;
private String processed;
}
Controller
@RestController
@RequestMapping("/v1/read")
public class ReadController{
@Autowired
private StatementBundleService bundleService;
@CrossOrigin
@GetMapping(value = "/statementBundles")
public List<A> listStatements() {
List<A> result = new ArrayList<A>();
List<B> bundles = bundleService.getAll();
for(B bundle: bundles) {
result.add(new A(bundle));
}
return result;
}
I am trying to figure out what is the best way return the list of A
without the properties "processed" from both class A
and class B
.
Should I only use for each
loop or iterator
? Also should I set properties to null
or some other approach?
I doubt if it is possible to alter the property without iterating it. Though you can try java8 for fast and simple output. Have a look in the soln.
public class Java8 {
public static void main(String[] args) {
List<Student> myList = new ArrayList<Student>();
myList.add(new Student(1, "John", "John is a good Student"));
myList.add(new Student(1, "Paul", "Paul is a good Player"));
myList.add(new Student(1, "Tom", "Paul is a good Teacher"));
System.out.println(myList);//old list
myList = myList.stream().peek(obj -> obj.setBiography(null)).collect(Collectors.toList());
System.out.println(myList);//new list
}
/*Output*/
//[Student [id=1, Name=John, biography=John is a good Student], Student [id=1, Name=Paul, biography=Paul is a good Player], Student [id=1, Name=Tom, biography=Paul is a good Teacher]]
//[Student [id=1, Name=John, biography=null], Student [id=1, Name=Paul, biography=null], Student [id=1, Name=Tom, biography=null]]
}
where Student class as is
public class Student{
private int id;
private String Name;
private String biography;
public Student(int id, String name, String biography) {
super();
this.id = id;
Name = name;
this.biography = biography;
}
public int getId() {
return id;
}
public String getBiography() {
return biography;
}
public void setBiography(String biography) {
this.biography = biography;
}
@Override
public String toString() {
return "Student [id=" + id + ", Name=" + Name + ", biography=" + biography + "]";
}
}