首页 > 解决方案 > 如何制作一个 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 请求中提到的方式一样

标签: javaspring-boot

解决方案


推荐阅读