I have a spring boot endpoint that returns a list of users. The users are populated into a DTO called User, however, some attributes of the user can be empty. Therefore how can I not send the empty attributes to the frontend?
@RequestMapping(value = "/users", produces = {"application/json"}, method = RequestMethod.GET) public List<User> getAllUsers() throws IOException { return kcAdminClient.getAllUsers(); } public class User { private String firstName; private String lastName; private String password; private String email; private String contactNumber; private String address; private String[] roles; } public List<User> getAllUsers() { int count = usersResource.count(); List<User> userList = new ArrayList<>(); List<UserRepresentation> userRepresentationList = usersResource.list();; for (UserRepresentation ur : userRepresentationList) { User user = new User(); user.setEmail(ur.getEmail()); user.setFirstName(ur.getFirstName()); user.setLastName(ur.getLastName()); userList.add(user); } return userList; } Returned values
{ "firstName": "test", "lastName": "test", "password": null, "email": "[email protected]", "contactNumber": null, "address": null, "roles": [ "test" ] } How can I stop the password and roles attribute from being sent as null?
Thanks in Advance