Java 从弹簧靴映射到角度映射
我有一个SpringBootRESTAPI,它向我发送一个带有键和值的映射,作为POJO,例如某个报告的映射。我试着把它放在有角度的前端。问题来了。 通常,当我得到像Collection angular这样的数组时,会自动将Doctor POJO从angular转换为我的Doctor类。但是对于贴图有一个问题,键没有被转换,它们只是作为字符串出现,即使值被正确解码,并且它们被转换到我的angular类 Rest控制器端点:Java 从弹簧靴映射到角度映射,java,angular,spring-boot,lombok,Java,Angular,Spring Boot,Lombok,我有一个SpringBootRESTAPI,它向我发送一个带有键和值的映射,作为POJO,例如某个报告的映射。我试着把它放在有角度的前端。问题来了。 通常,当我得到像Collection angular这样的数组时,会自动将Doctor POJO从angular转换为我的Doctor类。但是对于贴图有一个问题,键没有被转换,它们只是作为字符串出现,即使值被正确解码,并且它们被转换到我的angular类 Rest控制器端点: @GetMapping(value = "/doctors/
@GetMapping(value = "/doctors/reports")
public Map<HospitalDTO, DoctorsDTO> handleGroupDoctorsByHospital() {
var doctorsReport = service.groupDoctorsByHospital();
Map<HospitalDTO, DoctorsDTO> doctorsReportDto = new HashMap<>();
doctorsReport.forEach((hospital, doctors) -> doctorsReportDto.put(
hospitalConverter.convertModelToDto(hospital),
new DoctorsDTO(doctors.stream().map(doctorConverter::convertModelToDto).collect(Collectors.toSet()))
));
return doctorsReportDto;
}
医生:
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@Data
public class DoctorsDTO extends BaseDto{
Set<DoctorDTO> doctors;
}
角度等级:
export class Doctor{
id: number;
fullName: string;
practicingSince: string;
hospitalId: number;
constructor(id: number, fullName: string, practicingSince: string, hospitalId: number) {
this.id = id;
this.fullName = fullName;
this.practicingSince = practicingSince;
this.hospitalId = hospitalId;
}
}
我在这里处理请求:
getReport(): Observable<Map<Hospital, Doctor[]>>{
return this.http.get<Map<Hospital, Doctor[]>>(this.doctorURL + '/reports');
}
然而,我需要的关键也是我的角度类型的对象。即使在这个映射上进行迭代也无法完成,我会得到如下错误:响应没有forEach函数,或者响应没有forEach函数。values()不是iterable集
export class Doctor{
id: number;
fullName: string;
practicingSince: string;
hospitalId: number;
constructor(id: number, fullName: string, practicingSince: string, hospitalId: number) {
this.id = id;
this.fullName = fullName;
this.practicingSince = practicingSince;
this.hospitalId = hospitalId;
}
}
export class Hospital{
id: number;
capacity: number;
name: string;
address: string;
constructor(id: number, capacity: number, name: string, address: string) {
this.id = id;
this.capacity = capacity;
this.name = name;
this.address = address;
}
}
getReport(): Observable<Map<Hospital, Doctor[]>>{
return this.http.get<Map<Hospital, Doctor[]>>(this.doctorURL + '/reports');
}
{
"HospitalDTO(super=BaseDto(id=1), capacity=20, name=angular hospital, address=angular address)": {
"id": null,
"doctors": [
{
"id": 1,
"fullName": "testDoctor1",
"practicingSince": "2000-01-01",
"hospitalId": 1
}
]
},
"HospitalDTO(super=BaseDto(id=3), capacity=20, name=angular hospital 2, address=angular address 2)": {
"id": null,
"doctors": [
{
"id": 3,
"fullName": "front-end",
"practicingSince": "2018-02-01",
"hospitalId": 3
}
]
},
"HospitalDTO(super=BaseDto(id=2), capacity=20, name=second, address=second)": {
"id": null,
"doctors": [
{
"id": 2,
"fullName": "testDoctor2",
"practicingSince": "2000-02-01",
"hospitalId": 2
}
]
}
}