You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot中CrudRepository处理POST请求返回空JSON的解决方法

问题解决:新增图书后返回JSON字段为空

核心问题分析

从提供的代码来看,导致返回JSON全空的原因主要有两个:

  1. 缺少无参构造函数
    Hibernate和Spring Boot默认的JSON序列化器(Jackson)都要求实体类拥有无参构造函数,否则无法正确完成对象实例化、序列化/反序列化操作。你的Libro类仅定义了带参构造,未提供默认无参构造。

  2. 缺少Getter方法
    Jackson将对象序列化为JSON时,依赖字段的Getter方法读取值。你标注“省略getter和setter方法”,若实际未实现Getter,序列化时无法获取字段内容,自然返回空JSON。

修复步骤

1. 为Libro实体类添加无参构造函数

在Libro类中补充无参构造:

public Libro() {
    // 无参构造必须保留
}

2. 实现所有字段的Getter方法

为每个私有字段编写对应的Getter方法,示例:

public Integer getIdLibro() {
    return idLibro;
}

public String getIsbn() {
    return isbn;
}

public String getNombreLibro() {
    return nombreLibro;
}

// 其余字段的Getter方法同理实现

3. 可选:添加Setter方法(优化请求反序列化)

虽然@RequestBody可通过反射直接赋值字段,但添加Setter能让代码更规范,避免特殊场景下的赋值异常:

public void setIdLibro(Integer idLibro) {
    this.idLibro = idLibro;
}

public void setIsbn(String isbn) {
    this.isbn = isbn;
}

// 其余字段的Setter方法同理实现

修复后的完整Libro实体类示例

package com.libreria.proyect.libreria_el_aleph.model;

import jakarta.persistence.*; // Spring Boot 3+使用jakarta.persistence,旧版本用javax.persistence

@Entity
@Table(name = "Libro")
public class Libro {

    @Id
    @Column(name = "ID_libro")
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Integer idLibro;

    @Column(name = "ISBN")
    private String isbn;

    @Column(name = "Nombre_libro")
    private String nombreLibro;

    @Column(name = "Autor")
    private String autor;

    @Column(name = "Precio")
    private Double precio;

    @Column(name = "Cantidad")
    private int cantidad;

    // 无参构造
    public Libro() {
    }

    // 带参构造
    public Libro(Integer idLibro, String isbn, String nombreLibro, String autor, Double precio, int cantidad ){
        this.idLibro = idLibro;
        this.isbn = isbn; 
        this.nombreLibro = nombreLibro;
        this.autor = autor;
        this.precio = precio;
        this.cantidad = cantidad;
    }

    // Getter方法
    public Integer getIdLibro() {
        return idLibro;
    }

    public String getIsbn() {
        return isbn;
    }

    public String getNombreLibro() {
        return nombreLibro;
    }

    public String getAutor() {
        return autor;
    }

    public Double getPrecio() {
        return precio;
    }

    public int getCantidad() {
        return cantidad;
    }

    // Setter方法
    public void setIdLibro(Integer idLibro) {
        this.idLibro = idLibro;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public void setNombreLibro(String nombreLibro) {
        this.nombreLibro = nombreLibro;
    }

    public void setAutor(String autor) {
        this.autor = autor;
    }

    public void setPrecio(Double precio) {
        this.precio = precio;
    }

    public void setCantidad(int cantidad) {
        this.cantidad = cantidad;
    }
}

额外检查点

  • 确认项目已引入spring-boot-starter-web依赖(默认包含Jackson序列化组件)
  • 测试POST请求时,确保请求体JSON字段名与实体类字段名匹配;若存在命名差异,可通过@JsonProperty注解指定映射关系,例如:
    @JsonProperty("nombre_libro")
    private String nombreLibro;
    

内容的提问来源于stack exchange,提问作者Toms_Hd3z

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 17:25:58