Home > Blockchain >  Unrecognized token 'Payment': was expecting (JSON String, Number, Array, Object or token &
Unrecognized token 'Payment': was expecting (JSON String, Number, Array, Object or token &

Time:11-25

I am developing two Spring Boot microservices (order and payment) using H2 database order microservice calls the payment service through RestTemplate whenever I book an order using postman the payment service is called successfully and data is stored in the payment DB but not in the order service and I am receiving the above error:

here is my code

service

public TransactionResponse saveOrder(TransactionRequest request) {
    String responseMessage = "";
    Order order = request.getOrder();
    Payment payment = request.getPayment();
    payment.setOrderId(order.getId());
    payment.setAmount(order.getPrice());
    // response message
    System.out.println(order);
    // rest api call to payment service
    Payment paymentResponse =
           restTemplate.postForObject("http://localhost:8082/api/payment", payment, Payment.class);

    responseMessage = paymentResponse.getPaymentStatus().equals("success")?
           "payment processed successfully": "payment failed ";
    orderRepository.save(order);
    System.out.println(paymentResponse);

    return new TransactionResponse(order , paymentResponse.getTransactionId() , paymentResponse.getAmount(), responseMessage);
}

Controller

@PostMapping
public TransactionResponse bookOrder(@RequestBody TransactionRequest transactionRequest) {
    return orderService.saveOrder(transactionRequest);
}

Transaction Request model

@Data
@AllArgsConstructor
@NoArgsConstructor
public class TransactionRequest  {
  private Order order;
  private Payment payment;
}

payment model

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment {
    private int paymentId;
    private String paymentStatus;
    private String transactionId;
    private double amount;
    private int orderId;
}

images

the error iam recieving

postman request

CodePudding user response:

oh I got the problem since I was doing rest api call to the payment service the post method of the payment service was returning string instead of Payment object.

this was my post payment method before:

@PostMapping
public String orderPayment(@RequestBody Payment payment) {
   paymentService.doPayment(payment);
   return "Payment done";
}

this is what is how it is supposed to be

@PostMapping
public Payment orderPayment(@RequestBody Payment payment) {
   return paymentService.doPayment(payment);
}

note: the return type

  • Related