I am writing a spring boot api which fetches some data from db, store in model object & returns it. But I want to return only few fields of the model as api response.
List<MyModel> myModelList = new ArrayList<>();
mongoUserCollection.find().into(myModelList);
class MyModel{
public int id;
public String name;
public String lastname;
public int age;
// getter & setter of all properties
}
I am showing myModelList as response. In response its showing all the fields. How to show only specific fields like id and age only. Selecting only id & age from db will still show all fields of model in response (name & lastname will be shown as null). Is there any way apart from creating new ModelView class or setting JsonIgnore as this model?