#Springboot
1 messages · Page 1 of 1 (latest)
<@&987246527741304832> please have a look, thanks.
What does usersDao look like? And naming convention wise classes start with an uppercase.
usersDao
package com.sqltest.springServer.model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Streamable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class usersDao {
@Autowired
private userRepo repo;
public void save(users user) {
repo.save(user);
}
public List<users> getAllUsers(){
List<users> users = new ArrayList<users>();
Streamable.of(repo.findAll()).forEach(System.out::println);
return users;
}
public void delete(users user) {
repo.delete(user);
}
}
You're returning a new ArrayList, and just printing the result of your find all.
Streamable.of(repo.findAll()).forEach(System.out::println); i mean it print it in the console
but noy get it in postman
you mean the problem is this
public List<users> getAllUsers(){
List<users> users = new ArrayList<users>();
Streamable.of(repo.findAll()).forEach(System.out::println);
return users;
}
but im giving it ArrayList<users>(); the users
it doesnt?
do you know how a list/arraylist works in java?
List<users> users = new ArrayList<users>();
this just creates an empty list which can store users
well
your actual users that are stored get returned by repo.findAll()
but you only use that to print the users
not to return it to the caller of getAllUsers
so i should just delete it and return repo.findall()
do you know what an Iterable is?
send a get requrst and get data back
http://localhost:8080/users/get-all
using url and postman
no sorry
So now you don't link the two, you indeed just want to return the findAll result.
You're missing a variable assignment.
List<users> users = new ArrayList<users>();
Streamable.of(repo.findAll()).forEach(System.out::println);
Are 2 standalone lines without connection.
well lets me first explain the error
the repo.findAll() method returns an Iterable<users>, but your method says it returns List<users>
thats why it says "Incompatible types"
so changed to Iterable ?
yeah
so the question now would be, do you really want to return a List<users> or is it fine to return Iterable<users>
then you either change the return type or instead convert the iterable from repo.findAll() to a list
ok soo what im trying to do is
make the Spring boot Users for my android App [ android studio]
and connect and test loging and sign ups
so which one should i do
list
or
Iterable
just for testing and furture develope
that is kinda unrelated to what you are programming on the big scale
you need to look into the usage of the method
in your case the usage would be ResponseEntity.ok(users) where users is either the Iterable<users> or List<users> returned by the method
And in this case you don't even need to wrap it in ResponseEntity since you're doing nothing special header wise. You might be following an old tutorial.
yeah im following this
https://youtu.be/bqM1j5sCmHA?si=-SnWrNjI13okvZFd
2 years old
exact postman
In this tutorial, we will learn how to create REST API using spring boot. We will create two endpoints. One GET endpoint for getting all the employee objects from the server and one POST endpoint for accepting new employees that need to be saved.
For marking a class as a REST Controller, spring provides an annotation "@RestController". Then we ...
his code is different though
i tried hes code it gave
[]
in post man
and asked chatgpt
suggest this code
see this code, its a bit different to yours
it is not printing in the forEach
but instead adding to the list
so it doesnt return an empty list
but the content of repo.findAll() as a List
so you probably made a mistake copying someone elses code
which I would suggest isnt the best way to learn spring tbh
Looking at that code I wouldn't follow that video though.
there are really better resources to learn spring
That's a horrible way to write that.
no im not trying to learn im just trying to make the app work thats it
why
https://spring.academy/ is a good resource.
Compass
The new ArrayList is useless and wastes resources in this case, the repository can return a list, and there are better ways to go from an itterable to List.
which one is it
what do you suggest
public List<users> getAllUsers(){
List<users> users = new ArrayList<>();
Streamable.of(repo.findAll()).forEach(users::add);
return users;
}
yeah did fix it
getting data and postman now
but really what you guys suggest