在后功能弹簧上使用get方法

时间:2019-12-24 09:12:13

标签: spring-boot spring-restcontroller

我有一个问题,我有两个实体,DriverVehicle一对多, 我正在尝试添加新的车辆,但是当我想找司机时得到了null pointer exception

@PostMapping("/vehicles/{driverId}/{plateNumber}/{vehicleType}")
public Vehicle addDriver(@PathVariable int driverId, @PathVariable String plateNumber , @PathVariable String vehicleType) {

    // also just in case they pass an id in JSON ... set id to 0
    // this is to force a save of new item ... instead of update

    **Driver theDriver = driverService.findById(driverId);**
    System.out.println(theDriver.getFirstName());

    Vehicle theVehicle = new Vehicle();
    theVehicle.setId(0);

    theVehicle.setPlateNumber(plateNumber);
    theVehicle.setVehicleType(vehicleType);
    theVehicle.setDriver(theDriver);
    vehicleService.save(theVehicle);
    return theVehicle;

}

此方法在Driver Rest Controller中运行良好。

有人可以帮助我解决我的问题吗?

0 个答案:

没有答案