我正在尝试添加一个实体 Projet,它具有另一个实体 Sprint 的列表属性。这是两个实体
项目:
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
import java.util.List;
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Getter
@Setter
public class Projet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String title;
private String description;
@ManyToMany(mappedBy = "projets")
private List<User> users;
@OneToMany(mappedBy = "projet")
@JsonIgnore
private List<Sprint> sprints;
}
冲刺:
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
import java.util.Date;
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Sprint {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String description;
@Temporal(TemporalType.DATE)
private Date startDate;
@ManyToOne
@JsonIgnore
private Projet projet;
}
我有一个存储库接口,ProjetRepository:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import tn.esprit.spring.entities.Projet;
@Repository
public interface ProjetRepository extends JpaRepository<Projet, Integer> {
}
一个服务接口,IProjetService:
import tn.esprit.spring.entities.Projet;
public interface IProjetService {
public Projet addProject (Projet project);
}
还有一个服务实现类,ProjetService:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tn.esprit.spring.entities.Projet;
import tn.esprit.spring.entities.Sprint;
import tn.esprit.spring.repositories.ProjetRepository;
import java.util.List;
@Service
public class ProjetService implements IProjetService{
@Autowired
ProjetRepository projetRepository;
@Override
public Projet addProject(Projet projet) {
List<Sprint> sprints = projet.getSprints();
for ( Sprint sprint: sprints) {
sprint.setProjet(projet);
}
return projetRepository.save(projet);
}
}
这是控制器,ProjetController:
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tn.esprit.spring.entities.Projet;
import tn.esprit.spring.services.IProjetService;
@RequiredArgsConstructor
@RequestMapping("/projet")
@RestController
public class ProjetController {
@Autowired
IProjetService projetService;
@PostMapping("/addProjet")
public Projet addProject (@RequestBody Projet project){
return projetService.addProject(project);
}
}
这是测试应用,SpringTestApplication:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EntityScan(basePackages = {"tn.esprit.spring.entities"})
@ComponentScan(basePackages = {"tn.esprit.spring.controllers", "tn.esprit.spring.services"
, "tn.esprit.spring.repositories"})
@EnableJpaRepositories(basePackages = {"tn.esprit.spring.repositories"})
public class SpringTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringTestApplication.class, args);
}
}
使用此请求正文使用 Post 方法 http://localhost:8085/Exam/projet/addProjet 在此 url 上测试 Postman 上的 addProjet 方法时:
{
"title": "MAP",
"description": "Gestion de Mandats, Assignations et Projets",
"sprints": [
{
"description": "Sprint Projets",
"startDate": "2023-05-27"
}
]
}
在 Postman 响应正文中:
{
"timestamp": "2023-05-17T13:45:51.279+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/Exam/projet/addProjet"
}
在IDE控制台中:
java.lang.NullPointerException: null
at tn.esprit.spring.services.ProjetService.addProject(ProjetService.java:21) ~[classes/:na]
at tn.esprit.spring.controllers.ProjetController.addProject(ProjetController.java:20) ~[classes/:na]
这里指的是:
return projetRepository.save(projet);
我真的不明白问题是什么,有人可以帮忙吗?
编辑:我也在控制台中收到此错误:
2023-05-17 14:56:42.938 ERROR 8448 --- [nio-8085-exec-4] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [/Exam] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
你能在控制器中检查你的“Projet project”和“project.sprints”吗?他们不是空的吗?