【问题标题】:Java REST API stopped display java ListJava REST API 停止显示 java 列表
【发布时间】:2022-01-25 22:44:03
【问题描述】:

我编写了简单的 rest api,它必须显示 java 对象列表。因此,我使用 JPARepository(我在 BookService 类中实现)将两个书籍对象放入我的数据库中,现在我试图显示该信息。问题是,当我打开 localhost 页面时,它仅通过 {} 显示正确数量的对象,但是当我使用相同的方法在控制台中显示它时,它可以正常工作。

本地主机页面示例:

[{},{}]

我的代码:

@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 + '\'' +
                '}';
    }
}

我的休息控制器:

@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

【问题讨论】:

  • 什么是“停止”?什么是“控制台中的相同方法”?请显示:方法(index()),“在控制台中”,您在哪里/如何插入/什么是(一些示例)数据,哪个浏览器/curl/postman 地址,错误日志(如果有)!
  • @xerx593 ,已编辑:方法 index() 使用 JPARepository 中的 findAll() 方法,停止意味着在以前的项目中正常工作,在控制台中 - index().forEach(System.out::println)

标签: java rest


【解决方案1】:

现在明白问题所在了:

您有(肯定的)System.out -put,但 JSON 响应为空! (正确!?)

您“忘记了”getter 和 setter,因为(默认)json 映射就是这样工作的!

或者:

@JsonProperty

在田野上! ;)

对于更深入的研究(“钻”入):

【讨论】:

    【解决方案2】:

    您需要将 lombok 依赖添加到 pom.xml@Data 注释到您的实体。

     <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <optional>true</optional>
        </dependency>
    

    之后使用@Data 注释隐藏您的实体,如下所示:

    @Data
    @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;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-23
      • 2019-07-03
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      相关资源
      最近更新 更多