Spring Boot+GraphQL连接数据库返回NULL问题求助
问题描述
我想用Spring Boot + GraphQL API搭建个人作品集博客网站,展示各类编程语言项目的列表和详情。原本打算直接从数据库输出内容,后来改成API方式,但不知道怎么把数据库和GraphQL关联起来。现在通过/graphiql发起请求时,返回结果是:
{ "data": { "posts": null } }
我使用phpMyAdmin作为数据库管理工具,相关代码如下:
PostQuery.java
package com.example.demo.graphql; import java.util.List; import com.example.demo.models.Post; import com.example.demo.service.PostService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.coxautodev.graphql.tools.GraphQLQueryResolver; @Component public class PostQuery implements GraphQLQueryResolver { @Autowired private PostService service; public List<Post> getPosts() { return service.getAllPosts(); } public Post getPost(long id) { return service.getPost(id); } }
Post.java
package com.example.demo.models; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; @Entity public class Post { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String title, anons, full_text; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAnons() { return anons; } public void setAnons(String anons) { this.anons = anons; } public String getFull_text() { return full_text; } public void setFull_text(String full_text) { this.full_text = full_text; } public Post() { } public Post(String title, String anons, String full_text) { this.title = title; this.anons = anons; this.full_text = full_text; } }
PostRepository.java
package com.example.demo.repositorys; import com.example.demo.models.Post; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface PostRepository extends JpaRepository<Post, Long> { }
PostService.java
package com.example.demo.service; import com.example.demo.models.Post; import java.util.List; public interface PostService { public List<Post> getAllPosts(); public Post getPost(long id); public Post createPost(String title, String anons, String full_text); }
PostServiceImpl.java
package com.example.demo.service; import java.util.List; import com.example.demo.models.Post; import com.example.demo.repositorys.PostRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class PostServiceImpl implements PostService{ @Autowired PostRepository repository; public List<Post> getAllPosts() { return repository.findAll(); } public Post getPost(long id) { return repository.getById(id); } public Post createPost(String title, String anons, String full_text) { final Post post = new Post(); post.setTitle(title); post.setAnons(anons); post.setFull_text(full_text); return repository.save(post); } }
schema.graphqls
type Query { postById(id: ID): Post posts: [Post] } type Post { id: ID, title: String, anons: String, full_text: String }
排查建议
字段与方法名匹配问题
- GraphQL schema中查询字段是
posts,但PostQuery中的方法是getPosts()。graphql-tools要求方法名要和schema的查询字段严格对应,要么把方法名改成posts(),要么确认你的配置支持自动去掉get前缀的命名转换。 - schema中的
postById方法,对应PostQuery里的getPost(long id),方法名不匹配,也会导致该查询无法生效,需要改成postById(long id)。
- GraphQL schema中查询字段是
数据库连接与数据验证
- 检查
application.properties/application.yml中的数据库配置(URL、用户名、密码)是否正确,确保能正常连接到phpMyAdmin管理的数据库。 - 确认数据库中
post表(@Entity默认类名转小写为表名)是否存在数据,可在PostServiceImpl.getAllPosts()中添加日志,打印返回列表的大小,验证是否真的从数据库拿到了数据。
- 检查
Spring组件注入检查
- 确认
PostServiceImpl被Spring正确扫描并注入到PostQuery中,可在PostQuery.getPosts()中打印service是否为null,或者改用@RequiredArgsConstructor代替@Autowired,避免注入失败。
- 确认
JPA方法使用优化
PostServiceImpl.getPost(long id)中使用的repository.getById(id)是延迟加载代理对象,直接返回可能导致序列化问题,建议改成repository.findById(id).orElse(null),直接获取实体对象或null。
内容的提问来源于stack exchange,提问作者сергей иванов
相关产品推荐
相关产品推荐

