如何在 Spring Data JPA REST 中读取 post 请求的对象?

发布于 2025-01-14 19:38:40 字数 1788 浏览 1 评论 0原文

我正在使用 Spring Data JPA、REST 以及 H2 数据库。我能够创建数据和在命令行运行程序的帮助下保存到数据库,我可以使用GET请求读取数据。

但是我无法使用 POST 请求发布数据。

下面是我的控制器:

@PostMapping(value = {"/create"}, produces = "application/json",consumes = "application/json")
public Customer createCustomer(@RequestBody Customer cust) {
    System.out.println("new Customer to be added is :" + cust.toString());
    repository.save(cust);
    System.out.println("added Successfully");
    return cust;
}

有人可以帮助我吗?

客户实体看起来像:

@Entity
public class Customer {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;
    
    protected Customer() {
    }

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
    }

    public Long getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

尝试传递带或不带 id 的客户对象,但都不起作用。尝试传递

{"firstName":"Michelle","lastName":"Swepson"}

虽然​​获取我得到的所有内容,但

{"id":6,"firstName":null,"lastName":null} & the passed customer object is 

没有被 api 读取..因为我可以看到该对象为

{"id":null,"firstName":null,"lastName":null}

I am using Spring Data JPA, REST along with H2 database. I am able to create data & save to database with the help of commandline runner and i'm able to read the data with the GET request.

However i am not able to post the data using POST Request.

Here down is my Controller:

@PostMapping(value = {"/create"}, produces = "application/json",consumes = "application/json")
public Customer createCustomer(@RequestBody Customer cust) {
    System.out.println("new Customer to be added is :" + cust.toString());
    repository.save(cust);
    System.out.println("added Successfully");
    return cust;
}

Can anyone help me here?

Customer Entity looks like:

@Entity
public class Customer {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;
    
    protected Customer() {
    }

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
    }

    public Long getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

Tried passing customer object with and without id but none of them worked. tried passing

{"firstName":"Michelle","lastName":"Swepson"}

While fetching all i got is

{"id":6,"firstName":null,"lastName":null} & the passed customer object is 

Not getting read by api..as i could see the object as

{"id":null,"firstName":null,"lastName":null}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

早茶月光 2025-01-21 19:38:40

问题在于 RequestBody 注释。我确实使用了错误的 io.swagger.v3.oas.annotations.parameters.RequestBody 而不是 org.springframework.web.bind.annotation.RequestBody

issue was with RequestBody annotation.i did use the wrong one io.swagger.v3.oas.annotations.parameters.RequestBody instead of org.springframework.web.bind.annotation.RequestBody

肩上的翅膀 2025-01-21 19:38:40

问题出在实体上。您应该为属性 id 生成设置器。

public void setId(Long id) {
        this.id = id;
}

发送请求正文时,不要发送 id,因为它是自动生成的。

{"firstName":"Michelle","lastName":"Swepson"}

您还可以将 lombok 依赖项添加到您的项目中,以仅通过注释生成 getter、setter、构造函数等。

The issue is on the entity. You should generate setter for attribute id .

public void setId(Long id) {
        this.id = id;
}

When you send the request body, don't send the id because it is generate automatically.

{"firstName":"Michelle","lastName":"Swepson"}

You can also add the lombok dependency to your project to generate the getter, setter, constructors, etc. with just an annotation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文