java - Spring Boot在使用Spring数据JPA时抛出空指针异常
问题描述
我是 spring 的新手,当我尝试将实体保存到数据库时,它会引发空指针异常。以下是相关代码供参考:-
这是控制器:-
import com.project.newsblog.entities.Post;
import com.project.newsblog.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@Autowired
public PostService postService;
@PostMapping(value = "/post")
public String post(@RequestBody Post item){
System.out.println(item.toString());
postService.post(item);
return "Successful";
}
}
这是服务服务(错误的根本原因):-
import com.project.newsblog.entities.Post;
import com.project.newsblog.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PostService {
private PostRepository postRepository;
public List<Post> getAllPost(){
return postRepository.findAll();
}
public void post(Post post){
postRepository.save(post);
}
}
这是供参考的实体:-
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String creator;
private String title;
private String text;
private LocalDateTime dateTime;
public Post() {
}
@Override
public String toString() {
return "Post{" +
"id=" + id +
", creator='" + creator + '\'' +
", title='" + title + '\'' +
", text='" + text + '\'' +
", dateTime=" + dateTime +
'}';
}
public Post(String creator, String title, String text) {
this.creator = creator;
this.title = title;
this.text = text;
this.dateTime = LocalDateTime.now();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public LocalDateTime getDateTime() {
return dateTime;
}
public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}
}
将 json 有效负载保存到数据库时似乎存在问题。我想我错过了一些我无法弄清楚的东西。请帮帮我。这是最相关的调试消息:-
Post{id=1, creator='Someone', title='This is the title', text='This is the title', dateTime=null}
2021-06-15 22:52:22.145 DEBUG 14756 --- [nio-8080-exec-8] o.s.web.servlet.DispatcherServlet : Failed to complete request: java.lang.NullPointerException
2021-06-15 22:52:22.146 ERROR 14756 --- [nio-8080-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at com.project.newsblog.service.PostService.post(PostService.java:20) ~[classes/:na]
at com.project.newsblog.controller.MyController.post(MyController.java:21) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
编辑:- 当我使用@Autowired 在服务部分中注释 PostRepository 行时,它给出了错误:“没有 'com.project.newsblog.repository.PostRepository' 类型的合格 bean”。这是存储库代码:-
package com.project.newsblog.repository;
import com.project.newsblog.entities.Post;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
}
解决方案
推荐阅读
- php - 用一个 PHP 在 MySQL 中插入多条记录
- javascript - 如何使用javascript从解析平台检索具有键和值的for循环中的行
- python - 多次创建新的 TensorFlow 设备
- unix - PCB和PDB之间的区别或相似之处
- python - 如果其他 col 具有相同的值,则减去两个 df col
- elisp - Emacs Lisp 中的尾递归展平函数
- batch-file - 重命名日期格式的文件
- php - Google PHP SDK 未更改 setAccessToken 上的 Accesstoken
- asp.net - 列出 ASP .NET Core 中解决方案文件夹中的每个文件的问题
- arrays - 为每次迭代改变大小的变量预分配内存(长文本文件)