#Springboot

1 messages · Page 1 of 1 (latest)

austere minnow
#

it give me the data in the console log but not in Postman when i sent a get request

acoustic sapphireBOT
#

<@&987246527741304832> please have a look, thanks.

austere minnow
#

Consol Log

#

PostMan :

#

Uses Controllers:

fringe acorn
#

What does usersDao look like? And naming convention wise classes start with an uppercase.

austere minnow
# fringe acorn What does `usersDao` look like? And naming convention wise classes start with an...

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);
    }
}
fringe acorn
#

You're returning a new ArrayList, and just printing the result of your find all.

austere minnow
#

Streamable.of(repo.findAll()).forEach(System.out::println); i mean it print it in the console

#

but noy get it in postman

austere minnow
errant scarab
#

yea

#

the list users is empty at all times

#

you never fill it with stuff

austere minnow
#

but im giving it ArrayList<users>(); the users

errant scarab
#

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

austere minnow
#

well

errant scarab
#

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

austere minnow
#

so i should just delete it and return repo.findall()

errant scarab
#

most likely yea

#

but it depends on what you want to do

austere minnow
errant scarab
#

do you know what an Iterable is?

austere minnow
austere minnow
fringe acorn
#

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.

errant scarab
# austere minnow

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"

errant scarab
#

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

austere minnow
#

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

errant scarab
#

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

fringe acorn
#

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.

austere minnow
#

yeah im following this

#

https://youtu.be/bqM1j5sCmHA?si=-SnWrNjI13okvZFd

#

2 years old

#

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 ...

▶ Play video
errant scarab
austere minnow
#

and asked chatgpt
suggest this code

errant scarab
#

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

fringe acorn
#

Looking at that code I wouldn't follow that video though.

errant scarab
#

there are really better resources to learn spring

fringe acorn
#

That's a horrible way to write that.

austere minnow
#

no im not trying to learn im just trying to make the app work thats it

fringe acorn
fringe acorn
# austere minnow why

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.

austere minnow
austere minnow
#

getting data and postman now

#

but really what you guys suggest

errant scarab
#

I dont think there is a direct method to convert Iterable -> List

#

but you can just return Streamable.of(repo.findAll()).toList()