我有两种方法,一种是创建可以正常运行的单个用户,第二种是使用其他信息创建用户。我从用户那里获得的主要数据是从Postman回来的,但其他数据仍然为空。
为了创建具有更多信息的用户,我创建了新的RequestModel类,新的响应类which是TouristWithFlightRest,还有新的TouristWithFlightDTO。
为了实现这一点,我与两个实体建立了关系 FlightEntiti类
@ManyToOne
@JoinColumn(name = "tourists_id")
private TouristEntity touristList; // flights_id"
TouristEntity类
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "flights_id")
private List<FlightEntity> touristFlights;
public class TouristWithFlightRequestModel {
private String touristName;
private String touristLastName;
private String touristGender;
private String touristCountry;
private String touristNotes;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy", timezone = "GMT")
private Date touristBirthDay;
private List<FlightRequestModel> flights;
// FlightRequestModel is just simple class with getters/setters
// getters setters
}
响应类别
public class TouristWithFlightRest {
// REST -> CONVERT OUTGOING JAVA CLASS TO JSON, NEW TOURIST DATE
private String touristId;
private String touristName;
private String touristLastName;
private String touristGender;
private String country;
private String touristNotes;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy", timezone = "GMT")
private Date touristBirthDay;
private List<FlightRest> flightRests;
// getters, setters
}
DTO类
public class TouristWithFlightDTO implements Serializable {
private static final long serialVersionUID = -773069440206515124L;
private long id;
private String touristName;
private String touristLastName;
private String touristGender;
private String touristCountry;
private String touristNotes;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy", timezone = "GMT")
private Date touristBirthDay;
private String touristId;
private List<FlightDTO> flightsDTO;
}
DTO用于保存其他信息
public class FlightDTO implements Serializable {
private static final long serialVersionUID = 9132760496094706192L;
private long id;
private String flightId;
@JsonFormat(pattern = "dd-MM-yyyy HH:mm")
private Date dateArrival;
@JsonFormat(pattern = "dd-MM-yyyy HH:mm")
private Date dateDeparture;
private int planePlaces;
private double ticketPrice;
private TouristWithFlightDTO flightsDetails;
}
在界面服务中,我有方法:
p
ublic interface ITouristService {
TouristWithFlightDTO createSingleTouristWithFlight(TouristWithFlightDTO touristWithFlightDTO);
}
此方法的实现如下:
@Override
public TouristWithFlightDTO createSingleTouristWithFlight(TouristWithFlightDTO touristWithFlightDTO) {
if (itouristRepository.findByTouristId(touristWithFlightDTO.getTouristName()) != null) throw new RuntimeException("RECORD EXISTS");
ModelMapper modelMapper = new ModelMapper();
for(int i=0; i<touristWithFlightDTO.getFlightDTO().size(); i++) {
FlightDTO flights = touristWithFlightDTO.getFlightDTO().get(i);
flights.setFlightsDetails(touristWithFlightDTO);
flights.setFlightId(utils.flightIDGenerator(5));
touristWithFlightDTO.getFlightDTO().set(i, flights);
}
TouristEntity touristEntity = modelMapper.map(touristWithFlightDTO, TouristEntity.class);
String touristStringId = utils.touristIDGenerator(5);
touristEntity.setTouristId(touristStringId);
TouristEntity flightDetails = itouristRepository.save(touristEntity);
TouristWithFlightDTO returnDTO = modelMapper.map(flightDetails, TouristWithFlightDTO.class);
return returnDTO;
}
这是我的控制器类:
@PostMapping(“ /航班”) public TouristWithFlightRest createTouristWithFlight(@RequestBody TouristWithFlightRequestModel TouristWithFlightRequestModel){
TouristWithFlightRest returnValue;
if (touristWithFlightRequestModel.getTouristName().isEmpty()) throw new RuntimeException("ERROR in empty " + TouristWithFlightRest.class);
ModelMapper modelMapper = new ModelMapper();
TouristWithFlightDTO touristWithFlightDTO = modelMapper.map(touristWithFlightRequestModel, TouristWithFlightDTO.class);
TouristWithFlightDTO createTouristWithFlight = itouristService.createSingleTouristWithFlight(touristWithFlightDTO);
returnValue = modelMapper.map(createTouristWithFlight, TouristWithFlightRest.class);
return returnValue;
}
错误是空指针异常,它发生在实现类中:
for(int i=0; i<touristWithFlightDTO.getFlightDTO().size(); i++) {
// this is null
FlightDTO flights = touristWithFlightDTO.getFlightDTO().get(i);
flights.setFlightsDetails(touristWithFlightDTO);
flights.setFlightId(utils.flightIDGenerator(5));
touristWithFlightDTO.getFlightDTO().set(i, flights);
}
我的邮递员JSON如下所示:
{
"touristName": "NEwTourist",
"touristLastName": "ChcePodbijacKosmos",
"touristGender": "Obojniak",
"touristCountry": "ZSSR",
"touristNotes": "This is tourist notes for Russian",
"touristBirthDay": "15-12-1950",
"flights": [
{
"dateArrival": "10-05-2019 15:20",
"dateDeparture": "02-06-2018 18:49",
"planePlaces": "24",
"ticketPrice": "50.20"
},
{
"dateArrival": "10-05-2025 15:20",
"dateDeparture": "02-06-2025 18:49",
"planePlaces": "28",
"ticketPrice": "50.2"
}
]
}[![Project Structure][1]][1]
在调试时,我可以看到TouristName,lastName,Gender,Country,Notes,Birthday的设置没有任何问题。问题出在航班上。 我一直在寻找拼写错误等,但找不到任何错误。也许您可以在不使用for循环的情况下帮助我实现其他解决方案。
我的目标只是添加游客+航班作为附加信息。