Change in RequestMapping method or post addition of Optional in Java 8 from Java 7

I have been trying to follow a tutorial and in which they are basically using java 7 and currently I am working on a java 8 environment so I would want to know what would be java8 version of the code that I am trying to work on - currently I am getting alot of errors using Optional which was suggested by eclipse to fix a few initial errors but I am stuck on some.

The java7 code as below:

@RequestMapping("/reservations")
    public Reservation updateReservation1(ReservationUpdateRequest request) {
        Reservation reservation = reservationRepo.findOne(request.getId());
        reservation.setNumberOfBags((request.getNumOfBags());
        reservation.setCheckedIn(request.getCheckedIn());
        return reservationRepo.save(reservation);
    }

java8 with error

@RequestMapping("/reservations")
    public Optional<Reservation> updateReservation(ReservationUpdateRequest request) {
        Optional<Reservation> reservation = reservationRepo.findById(request.getId());
        reservation.setNumberOfBags((request.getNumOfBags());
        reservation.setCheckedIn(request.getCheckedIn());
        return reservationRepo.save(reservation);
    }

here in java8 code eclipse ide is giving me error at line number 3 - that is when I am trying to set value into reservation.setNumberOfBags() it shows The method setNumberOfBags(int) is undefined for the type Optional<Reservation> - if any one can help ?

2 answers

  • answered 2019-04-21 09:49 f.khantsis

    @RequestMapping("/reservations")
    public Optional<Reservation> updateReservation(ReservationUpdateRequest request) {
        Optional<Reservation> reservation = reservationRepo.findById(request.getId());
        if(reservation.isPresent()) {
            Reservation actReservation = reservation.get();
            actReservation .setNumberOfBags((request.getNumOfBags());
            actReservation .setCheckedIn(request.getCheckedIn());
            return reservationRepo.save(actReservation );
        }
        else {
            return Optional.empty();
        }
    }
    

  • answered 2019-04-21 10:11 Patrice Blanchardie

    You should return Reservation instead of Optional<Reservation> (I guess you're using Spring Data, see the save method).

    If you want to throw an exception when the reservation is not found, use:

    public Reservation updateReservation(ReservationUpdateRequest request) {
            Reservation reservation = reservationRepo.findById(request.getId())
                .orElseThrow(() -> new EntityNotFoundException("Reservation not found"));
            reservation.setNumberOfBags(request.getNumOfBags());
            reservation.setCheckedIn(request.getCheckedIn());
            return reservationRepo.save(reservation);
        }
    

    In case you want to return a 404 error code when it's not found:

    public ResponseEntity<Reservation> updateReservation(ReservationUpdateRequest request) {
         return reservationRepo.findById(request.getId())
             .map(r -> {
                  r.setNumberOfBags(request.getNumOfBags());
                  r.setCheckedIn(request.getCheckedIn());
                  return ResponseEntity.ok().body(reservationRepo.save(r));
              }).orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
        }
    

    And the RequestMapping is weird, you should use PUT with the id in the URL instead of request body.

    @PutMapping("/reservations/{id}")
    public ResponseEntity<Reservation> updateReservation(@PathParam("id") Long id, ReservationUpdateRequest request) {
         return reservationRepo.findById(id)
             .map(r -> {
                 r.setNumberOfBags(request.getNumOfBags());
                 r.setCheckedIn(request.getCheckedIn());
                 return ResponseEntity.ok().body(reservationRepo.save(r));
             }).orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }