小编Azh*_*zad的帖子

如何使用 Arrays.asList() 方法返回对象列表?

我正在使用 Spring-boot 设置一个 RestController。这个项目要求我返回一个对象列表(在这种情况下是类 Book 的对象)。我怎么做?

我已经通过传递类 Book 的对象来尝试 Arrays.asList() 方法,如下所示:

爪哇

@RestController
public class BookController {

    @GetMapping("/books")
    public List<Book> getAllBooks() {

        return Arrays.asList(new Book(1l, "Book name", "Book author"));

    }
}
Run Code Online (Sandbox Code Playgroud)

爪哇

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 …
Run Code Online (Sandbox Code Playgroud)

java spring jpa

1
推荐指数
1
解决办法
1495
查看次数

为什么在通过data.sql文件导入时会出现这个错误“Error created bean with name 'entityManagerFactory' defined in class path resource”

我正在设置一个 spring-boot+hibernate 项目。我创建了一个模型类“People”,并在运行项目 h2-console 后按预期创建了一个表“people”。然后我在资源文件夹中创建了一个 data.sql 文件,并在表中添加了一个导入语句。之后程序无法运行并显示以下错误。

Hibernate: 

    create table people (
       id bigint not null,
        age integer not null,
        job varchar(255),
        name varchar(255) not null,
        primary key (id)
    )
2019-10-16 15:41:34.911  INFO 7540 --- [           main] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@fe09383'
2019-10-16 15:41:34.916  INFO 7540 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-10-16 15:41:34.963  WARN 7540 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa h2 spring-boot

1
推荐指数
1
解决办法
668
查看次数

标签 统计

java ×2

jpa ×2

h2 ×1

hibernate ×1

spring ×1

spring-boot ×1