I'm setting up a RestController using Spring-boot. This project requires me to return a list of object (in this case objects of class Book). How do I do that?
I've tried Arrays.asList() method by passing a object of class Book shown below:
java
@RestController
public class BookController {
@GetMapping("/books")
public List<Book> getAllBooks() {
return Arrays.asList(new Book(1l, "Book name", "Book author"));
}
}
java
public class Book {
Long id;
String name;
String author;
public Book(Long id, String name, String author) {
super();
this.id = id;
this.name = name;
this.author = author;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", author=" + author + "]";
}
}
I've got this error "Type mismatch: cannot convert from List<Object> to List<Book>". How can I fix this?
Arrays.asListwork?import java.util.*;and commenting out the jpa annotation makes it compile fine withjavac -Xlint:all *.java. (Also, useList.ofsince Java SE 9.)