Home > OS >  Java REST API stopped display java List
Java REST API stopped display java List

Time:12-27

I wrote simple rest api which has to display java list of objects. So I put two object of books into my database using JPARepository (which I implement in my BookService class) and now im trying to display that information. The problem is that when Im opening localhost page it displays only correct quantity of objects by {}, but when I use same method to display it in console it works correctly.

Example of localhost page:

[{},{}]

my code:

@Entity(name = "Book")
@Table(name = "book")
public class Book {
    @Id
    @Column(name = "id", updatable = false)
    private int id;
    @Column(name = "title", nullable = false, columnDefinition = "TEXT")
    private String title;
    @Column(name = "author", nullable = false, columnDefinition = "TEXT")
    private String author;
    @Column(name = "publisher", nullable = false, columnDefinition = "TEXT")
    private String publisher;

    public Book(int id, String title, String author, String publisher) {
        this.id = id;
        this.title = title;
        this.author = author;
        this.publisher = publisher;
    }

    public Book() {}

    @Override
    public String toString() {
        return "Book{"  
                "id="   id  
                ", title='"   title   '\''  
                ", author='"   author   '\''  
                ", publisher='"   publisher   '\''  
                '}';
    }
}

My rest controller:

@RestController
@RequestMapping("library/books")
public class BookController {
    private final BookService bookService;

    public BookController(BookService bookService) {
        this.bookService = bookService;
    }

    // localhost:8004/library/books
    @GetMapping()
    public List<Book> getAllBooks() {
        return bookService.index();
    }
}

    @Override
    public List<Book> index() {
    return bookRepository.findAll();
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.company</groupId>
    <artifactId>simple-library</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>simple-library</name>
    <description>simple-library</description>
    <properties>
        <java.version>16</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.24</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

GET http://localhost:8007/library/books/

HTTP/1.1 200 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Sun, 26 Dec 2021 15:44:11 GMT
Keep-Alive: timeout=60
Connection: keep-alive

[
  {},
  {}
]

Response code: 200; Time: 329ms; Content length: 7 bytes

CodePudding user response:

Now understand the problem:

You have (positive) System.out -put, but empty JSON response! (correct!?)

You "forgot" getters and setters, since that is how the (default) json mapping works!

Alternatively:

@JsonProperty

on the fields! ;)

For deeper studies ("drill" into):

  • Related