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

SpringBoot中GET请求返回空对象问题排查求助

问题解决:SpringBoot GET接口返回空对象但POST正常

问题描述

开发了一个与React前端交互的SpringBoot应用,使用Postman发起GET请求后,响应返回空对象而非数据库中的目标数据。POST请求可正常执行,项目编译无错误,所有文件位于同一包下,数据库表中存在两行对应数据,但接口始终无法返回有效内容。

代码片段

实体类

@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "game_articles")
public class Articles {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "title")
    private String title;

    @Column(name = "reviewer")
    private String reviewer;

    @Column(name = "review")
    private String review;

    @Column(name = "path")
    private String path;

    @Column(columnDefinition = "integer default 0")
    private int comments;

    public Articles(String title, String reviewer, String review, String path, int comments){
        this.title = title;
        this.reviewer = reviewer;
        this.review = review;
        this.path = path;
        this.comments = comments;
    }
}

控制器

@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping("/api/v1")
@RestController
@AllArgsConstructor
public class ArticlesController {

    @Autowired
    ArticlesRepository articlesRepository;

    @GetMapping("/articles")
    public List<Articles> getAllArticles(){
//        createArticle();
        return articlesRepository.findAll();
    }

    public void createArticle(){ //忽略此方法
    Articles a = new Articles(1L,"Title Test", "TyTest", "This is a test review", "path/totest", 10);
        articlesRepository.save(a);
    }

//    @GetMapping("/articles")
//    public ResponseEntity<List<Articles>> getAllArticles(){
//        try {
////            articlesRepository.findAll().forEach(articles::add);
//            return new ResponseEntity<>(articles, HttpStatus.OK);
//        }
//        catch (Exception e){
//            return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
//        }
//    }
}

仓库

@Repository
public interface ArticlesRepository extends JpaRepository<Articles, Long> {
}

日志中的SQL查询

Hibernate: 
    select
        articles0_.id as id1_0_,
        articles0_.comments as comments2_0_,
        articles0_.path as path3_0_,
        articles0_.review as review4_0_,
        articles0_.reviewer as reviewer5_0_,
        articles0_.title as title6_0_ 
    from
        game_articles articles0_

问题根源

你的Articles实体类缺少getter方法。SpringBoot默认使用Jackson将实体对象序列化为JSON返回,Jackson需要通过getter方法访问对象的属性值。POST请求正常是因为创建对象时可以通过构造器赋值,但查询返回实体时,没有getter就无法读取属性,最终返回空JSON对象。

另外,日志中的articles0_是Hibernate自动生成的表别名,属于正常查询逻辑,不影响数据读取。

解决方案

方案1:使用Lombok自动生成getter(推荐)

在实体类上添加@Data注解(需确保项目已引入Lombok依赖),该注解会自动生成所有属性的getter、setter,以及toString、equals、hashCode等方法:

@Data
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "game_articles")
public class Articles {
    // 原有属性和构造器保持不变
}

方案2:手动添加getter方法

如果不想使用Lombok,手动为每个属性添加getter方法:

public Long getId() {
    return id;
}

public String getTitle() {
    return title;
}

public String getReviewer() {
    return reviewer;
}

public String getReview() {
    return review;
}

public String getPath() {
    return path;
}

public int getComments() {
    return comments;
}

修改后重启项目,再次用Postman发起GET请求,即可正常返回数据库中的数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 10:52:49