Home > Mobile >  Spring Boot - Return an empty array of objects
Spring Boot - Return an empty array of objects

Time:11-15

I'm trying to perform a simple GET and when I run and insert the directory it returns an empty array:

This is my class:

public class Movie {
String directory;
String name;
boolean isPlaying;
int watchedCounter;

public Movie(String directory, String name, boolean isPlaying, int watchedCounter) {
    this.directory = directory;
    this.name = name;
    this.isPlaying = isPlaying;
    this.watchedCounter = watchedCounter;
} }

My Controller:

@RestController
@RequestMapping(path = "/api/vod")
public class MovieConroller{
    private final MovieService movieService;

    @Autowired
    public MovieConroller(MovieService movieService) {
        this.movieService = movieService;
    }

    @GetMapping
    public List<Movie> getMovies(){
        return movieService.getMovies();
    } }

My Service:

@Service
public class MovieService{
    public List<Movie> getMovies(){
        List<Movie> movies = new ArrayList<>();
        movies.add(new Movie("home", "film.avi", true, 0));
        movies.add(new Movie("house", "fil2m.avi", false, 1));
        return movies;
    }
}

And this is my application properties:

spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=ad
spring.data.mongodb.password=pas
spring.data.mongodb.database=mov
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false

The URL : http://localhost:8080/api/vod Result : [{}{}]

Can someone please assists? Regards

CodePudding user response:

The problem is the lack of getters and setters.

Usually it thrown an error, but using spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false it returns {} because can't serialize the objects.

If you add getters and setters it will return the list with objects filled.

  • Related