Home > front end >  Spring Boot JPA returns correct count but no data
Spring Boot JPA returns correct count but no data

Time:12-01

Evening,

I have a Spring application that is connected to a PostgresSQL db. I can connect to the database and see that the query is returning the correct number of elements for the array but nothing in them:

curl http://localhost:8080/books
[{},{},{}]%

My Book model looks like this:


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.math.BigDecimal;

@Entity
public class Book {
  
  @Id
  @GeneratedValue
  private Long id;
  
  private String name;
  private String author;
  private BigDecimal price;

  public Book() {}

  public Book(String name, String author, BigDecimal price) {
    this.name = name;
    this.author = author;
    this.price = price;
  }
}

and the controller:

@RestController
public class BookController {

  @Autowired
  private BookRepository repository;

  // Find
  @GetMapping("/books")
  List<Book> findAll() {
    List<Book> books = repository.findAll();
    System.out.println(books);

    return repository.findAll();
  }

}

I've looked at these questions here, here and here but those answers didn't fit with this.

What am I not doing to see data come back?

CodePudding user response:

In order for your entity to be serialized by Spring the entity needs to have getters for its properties. You could use lombok to auto-generate getter/setters for you entity properties or just write them your own.

  • Related