Spring Boot 控制器中的ID为空

8wtpewkr  于 2023-03-02  发布在  Spring
关注(0)|答案(1)|浏览(200)

我用java开发。在服务层,我从数据库中获取值,如“diagnosisTitle”、“givenDate”等。在报告实体中,我也将OneToManyMap到Laboratorian和Patient。当findById函数调用报告值时,它工作正常。但在控制器端,Laboratorian id和Patient id为空。
这里是我的服务:

public ReportResponse findReportById(Long id) {
        Optional<Report> report = reportRepository.findById(id);
        if(report.isPresent()){
            return modelMapper.map(report.get(), ReportResponse.class);
        }
        return null;
    }

这是我的控制器:

@GetMapping("/findById/{id}")
    public ResponseEntity<ReportResponse> findReportById(@PathVariable("id") Long id){

        ReportResponse resultReport = reportService.findReportById(id);
            return ResponseEntity.ok(resultReport);
    }

下面是我发送GET本地主机:8080/report/findById/102时的 Postman 输出:

{
    "fileNo": 102,
    "diagnosisTitle": "Kriz",
    "diagnosis": "Açıklama",
    "givenDate": "2023-02-27T08:07:41.382+00:00",
    "imageName": null,
    "labIdNo": null,
    "patientId": null
}

我想我在模型Map上犯了一个错误。

envsm3lx

envsm3lx1#

好的,我通过使用ModelMapper对象的map()方法将Laboratorian和Patient实体Map到它们的ID来解决这个问题。

public ReportResponse findReportById(Long id) {
Optional<Report> report = reportRepository.findById(id);
if(report.isPresent()){
    ReportResponse reportResponse = modelMapper.map(report.get(), ReportResponse.class);
    reportResponse.setLaboratorianId(report.get().getLaboratorian().getId());
    reportResponse.setPatientId(report.get().getPatient().getId());
    return reportResponse;
}
return null;}

相关问题