UserServiceImpl.java
@Override public List<User> getAllUsers() { User user = new User(); List<User> ulist = new ArrayList<>(); String query = "select * from user"; try { dataBaseConnection.pstmt = dataBaseConnection.conn.prepareStatement(query); dataBaseConnection.rs = dataBaseConnection.pstmt.executeQuery(); while ( dataBaseConnection.rs.next() ) { ulist.add(new User(dataBaseConnection.rs.getInt(1), dataBaseConnection.rs.getString(2), dataBaseConnection.rs.getString(3), dataBaseConnection.rs.getString(4), dataBaseConnection.rs.getString(5), dataBaseConnection.rs.getString(6), dataBaseConnection.rs.getInt(7), dataBaseConnection.rs.getInt(8) )); } System.out.println("getting all users successfully..."); } catch (Exception e) { e.printStackTrace(); } return ulist; }
Controller Class name UserOperationController.java
@GetMapping("/get-all-user") public List<User> fetchUsers() { List<User> users = new ArrayList<User>(); users = this.userService.getAllUsers(); return users; }
I'm trying to store user information in a list but instead of storing user info list stores memory objects of user class. Here User is @Entity which has getters and setters. When call Get Mapping urlit produces output like:
[ {"userName": null,"userEmail": null,"userPassword": null,"userState": null,"userCity": null,"userId": 0,"userAge": 0,"userMobile": 0 }, {"userName": null,"userEmail": null,"userPassword": null,"userState": null,"userCity": null,"userId": 0,"userAge": 0,"userMobile": 0 }]
instead of null values I'm expecting value from database but it produces output null values. Help me to find the correct answer.