communication using rest template is not getting established

i have two services bookingService and payment Service. I have an end point in booking service ”
http://localhost:9191/hotel/booking/2/transaction” which uses rest template to connect to transaction service. booking service is using “http://localhost:8082/payment/transaction” end point of transaction service. individually “http://localhost:8082/payment/transaction” endpoint is working fine but when it is called from booking service, it shows error.

            package com.example.bookingservice.controller;

            import com.example.bookingservice.dto.BookingRequestDto;
            import com.example.bookingservice.dto.BookingResponseDto;
            import com.example.bookingservice.dto.TransactionRequestDto;
            import com.example.bookingservice.entity.Booking;
            import com.example.bookingservice.exceptions.InvalidPaymentModeException;
            import com.example.bookingservice.service.BookingService;
            import com.example.bookingservice.service.BookingServiceImpls;

            import com.fasterxml.jackson.databind.ObjectMapper;
            import org.modelmapper.ModelMapper;
            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.http.HttpStatus;
            import org.springframework.http.MediaType;
            import org.springframework.http.ResponseEntity;
            import org.springframework.web.bind.annotation.*;
            import org.springframework.web.client.HttpClientErrorException;
            import org.springframework.web.client.HttpServerErrorException;
            import org.springframework.web.client.RestTemplate;

            import java.time.LocalDateTime;
            import java.util.HashMap;
            import java.util.Map;

            @RestController
            @RequestMapping(value = "/hotel")
            public class BookingController {


                @Autowired
                ModelMapper modelMapper;

                @Autowired
                BookingService bookingService;

                @Autowired
                RestTemplate restTemplate;


                @PostMapping(value = "/booking", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
                public ResponseEntity createBooking(@RequestBody BookingRequestDto bookingRequestDto){

                    Booking requestBooking =modelMapper.map(bookingRequestDto, Booking.class);
                    BookingResponseDto bookingResponseDto = new BookingResponseDto();
                    //        extracted from bookingRequestDTO
                    bookingResponseDto.setFromDate(requestBooking.getFromDate());
                    bookingResponseDto.setToDate(requestBooking.getToDate());
                    bookingResponseDto.setAadharNumber(requestBooking.getAadharNumber());


                    //        adding additional parameters
                    bookingResponseDto.setRoomNumbers(bookingService.getRandomNumbers(requestBooking.getNumOfRooms()));
                    bookingResponseDto.setRoomPrice(bookingService.roomCost(requestBooking.getFromDate()
                            ,requestBooking.getToDate(), requestBooking.getNumOfRooms()));
                    bookingResponseDto.setTransactionId(0);
                    bookingResponseDto.setBookedOn(LocalDateTime.now());


                    Booking responseBooking = modelMapper.map(bookingResponseDto, Booking.class);

                    responseBooking.setNumOfRooms(bookingRequestDto.getNumOfRooms());
                    Booking savedBooking = bookingService.createBooking(responseBooking);

                    bookingResponseDto.setBookingId(savedBooking.getBookingId());



                    return new ResponseEntity(bookingResponseDto, HttpStatus.CREATED);

                }

                @GetMapping(value="/{id}/aadhar")
                public ResponseEntity<String> getMovieBasedOnId(@PathVariable(name="id") int id){
                    Booking responseBooking = bookingService.getBookingBasedOnId(id);

                    String bookingAadharNumber = responseBooking.getAadharNumber();

                    return new ResponseEntity(bookingAadharNumber, HttpStatus.OK);
                }



                @PostMapping(value = "/booking/{id}/transaction", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
                public ResponseEntity<BookingResponseDto> completePayment(@PathVariable(name = "id") int id, @RequestBody TransactionRequestDto transactionRequestDto) {
                    // Retrieve the booking based on the provided ID
                    Booking requestedBooking = bookingService.getBookingBasedOnId(id);

                    // Ensure that the transactionRequestDto contains the necessary data
                    System.out.println("Payment Request: " + transactionRequestDto.toString());

                    // Define the URL for the Payment Service
                    String transactionGet_IdUrl = "http://localhost:8082/payment/transaction";

                    // Make the POST request to the Payment Service
                    ResponseEntity<Integer> transactionResponse = restTemplate.postForEntity(transactionGet_IdUrl, transactionRequestDto, Integer.class);

                    try {
                        if (transactionResponse.getStatusCode() == HttpStatus.CREATED) {
                            int transactionId = transactionResponse.getBody();
                            System.out.println("Transaction ID: " + transactionId);

                            // Update the booking with the transaction ID
                            requestedBooking.setTransactionId(transactionId);
                            bookingService.updateBooking(id, requestedBooking);

                            // Map the updated booking to a response DTO
                            BookingResponseDto bookingResponseDto = modelMapper.map(requestedBooking, BookingResponseDto.class);

                            return new ResponseEntity<>(bookingResponseDto, HttpStatus.FOUND);
                        } else {
                            // Handle the case where the payment transaction was not successful
                            return ResponseEntity.status(transactionResponse.getStatusCode()).build();
                        }
                    } catch (Exception e) {
                        // Handle exceptions that may occur during the request
                        e.printStackTrace();
                        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
                    }
                }

            }




            @RestController
            @RequestMapping(value = "/payment")
            public class TransactionController {

                @Autowired
                TransactionService transactionService;

                @Autowired
                RestTemplate restTemplate;

                @Autowired
                ModelMapper modelMapper;

                @PostMapping(value = "/transaction", consumes = MediaType.APPLICATION_JSON_VALUE)
                public ResponseEntity<Integer> performTransaction(@RequestBody TransactionRequestDto transactionRequestDto) {

                    Transaction requestedTransaction = new Transaction();
                    requestedTransaction.setPaymentMode(transactionRequestDto.getPaymentMode());
                    requestedTransaction.setBookingId(transactionRequestDto.getBookingId());
                    requestedTransaction.setUpiId(transactionRequestDto.getUpiId());
                    requestedTransaction.setCardNumber(transactionRequestDto.getCardNumber());


     //        Transaction requestedTransaction = modelMapper.map(transactionRequestDto, Transaction.class);

                    Transaction savedTransaction = transactionService.saveTransaction(requestedTransaction);

                    int transactionId = transactionService.generateTransactionId(requestedTransaction);

                    return new ResponseEntity<>(transactionId, HttpStatus.CREATED);

                }

                @GetMapping(value = "/{id}")
                public ResponseEntity<TransactionDto> getTransactionDetails(@PathVariable (name = "id") int id){

                    Transaction transaction = transactionService.getTransactionFromId(id);

                    if(transaction == null){
                        return ResponseEntity.notFound().build();
                    }

                    TransactionDto transactionDto = modelMapper.map(transaction, TransactionDto.class);

                    return ResponseEntity.ok(transactionDto);


                }


            }

  • it shows error. would be good to know which error

    – 

  • { “type”: “about:blank”, “title”: “Bad Request”, “status”: 400, “detail”: “Failed to read request”, “instance”: “/hotel/booking/1/transaction” }

    – 

  • Looks like the request is incorrect

    – 

  • public class TransactionRequestDto { private String paymentMode; private int bookingId; private String upiId; private String cardNumber; } i have this TransactionRequestDto defined in both services, request is: { “paymentMode”: “UPI”, “bookingId”: 1, “upiId”: “upi details”, “cardNumber”: “65757668687” } @Jens

    – 




  • @Jens There exists entry in database with booking id 1

    – 




Leave a Comment