java - 如何制作一个 Restful API 来处理 Spring Boot 中的多对多关系?
问题描述
在我的 Spring Boot 项目中,我有两个表名——医生和病人。在这些表中,我具有以下属性-
现在,问题是我想在这两个表之间创建多对多关系以进行预约,因为一位医生可以有很多患者,而一位患者可以预约多位医生。所以,为了解决这个问题,我创建了另一个名为约会的表,其中有医生 ID 和病人 ID 作为外键。
我需要使用 JSON 请求正文来创建约会,如下所示 -
因此,为此我创建了一个模型类,如下所示 -
约会.java
@Entity
@Table(name = "appointments")
public class Appointment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private Long appointedDoctorId;
@NotNull
private Long appointedPatientId;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "doctorId", nullable = false)
private Doctor doctor;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "patientId", nullable = false)
private Patient patient;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getAppointedDoctorId() {
return appointedDoctorId;
}
public void setAppointedDoctorId(Long appointedDoctorId) {
this.appointedDoctorId = appointedDoctorId;
}
public Long getAppointedPatientId() {
return appointedPatientId;
}
public void setAppointedPatientId(Long appointedPatientId) {
this.appointedPatientId = appointedPatientId;
}
public Doctor getDoctor() {
return doctor;
}
public void setDoctor(Doctor doctor) {
this.doctor = doctor;
}
public Patient getPatient() {
return patient;
}
public void setPatient(Patient patient) {
this.patient = patient;
}
}
这是存储库类-
约会存储库.java
@Repository
public interface AppointmentRepository extends JpaRepository<Appointment, Long> {
Page<Appointment> findByDoctorId(Long id, Pageable pageable);
}
这是我的服务界面-
预约服务.java
public interface AppointmentService {
public Page<Appointment> getAllAppointmentByDoctorId(@PathVariable(value = "doctorId") Long id, Pageable pageable);
public Appointment createAppointment(@Valid @RequestBody Appointment createAppointmentRequest);
}
这是我的服务类的实现-
AppointmentServiceImpl.java
@Service
public class AppointmentServiceImpl implements AppointmentService {
@Autowired
AppointmentRepository appointmentRepository;
@Override
public Page<Appointment> getAllAppointmentByDoctorId(Long id, Pageable pageable) {
return appointmentRepository.findByDoctorId(id, pageable);
}
@Override
public Appointment createAppointment(@Valid Appointment createAppointmentRequest) {
return appointmentRepository.save(createAppointmentRequest);
}
}
最后我有了这个控制器类-
约会.java
@RestController
@RequestMapping("/api")
public class AppointmentController {
@Autowired
AppointmentService appointmentService;
@GetMapping("/doctors/{doctorId}/appointments")
public Page<Appointment> getAllAppointmentsByDoctorId(@PathVariable(value = "id") Long id, Pageable pageable){
return appointmentService.getAllAppointmentByDoctorId(id, pageable);
}
@PostMapping("/insert/new/appointments")
public Appointment createAppointment(@Valid Appointment createAppointmentRequest) {
return appointmentService.createAppointment(createAppointmentRequest);
}
}
但是,每当我运行项目并使用上述请求正文执行 POST 请求以创建约会时,它都会显示以下响应-
所以,我需要知道我的代码中有什么问题,以及如何通过提供医生 ID 和患者 ID 来创建预约POST 请求,就像我在JSON RequestBody 请求中提到的方式一样。
解决方案
推荐阅读
- reactjs - 如何为 React App 应用 antd 深色主题?
- android - Flutter - 拉动以刷新网页视图?
- r - 将矩阵与R中的向量等同是什么意思
- java - 如何管理数组中的概率?
- java - 如何在我的 React JHipster 应用程序中添加 fontawesome 图标?
- mysql - 您能否解释使用 Composite MySQL INDEX 进行多个 INNER JOIN 的方面?
- php - 包括'count.html.php'
- angular - 有没有办法访问 templateRef 中的内容
- wordpress - 如何使用 WPBakery 的 vc_map() 动态显示其他参数
- templates - 如何防止 MediaWiki 中#if 条件内的图像“隐藏”链接