I have a post service with collection on Postman. I can successfully get a response with Postman. This service is a bit complicated with its body and headers. I want to call this service from SpringBoot using Resttemplate. But I am getting the error mentioned in the title. I'm probably making a mistake somewhere in the request list or sending the wrong data type, but I have no idea and I've been trying for a long time. I hope you can help me. First of all, I will share the raw body that I post request with Postman:
{
"currency": "EUR",
"customer_country": "NL",
"customer_language": "en",
"partner_metadata": {
"sales_channel": "inPath",
"device":"app"
},
"request": [
{
"policy_type": "comprehensive_travel_insurance",
"policy_type_version": "7",
"is_return": false,
"policy_start_date": "2022-06-07T10:22:30.925568 01:00",
"policy_end_date": "2022-06-13T20:55:00 02:00",
"departure_country": "LT",
"destination_country": "EE",
"total_tickets_price": 78.99,
"number_of_adults": 1,
"number_of_children": 0,
"number_of_infants": 0,
"trip_start_date": "2022-06-13T19:45:00 02:00",
"trip_end_date": "2022-06-13T20:55:00 02:00",
"flights": [
{
"legs": [
{
"departure_datetime": "2022-06-13T19:45:00 02:00",
"arrival_datetime": "2022-06-13T20:55:00 02:00",
"flight_number": "BT905",
"marketing_airline_iata_code": "BT",
"operating_airline_iata_code": "BT",
"departure_airport": "VNO",
"arrival_airport": "TLL",
"departure_country": "LT",
"arrival_country": "EE"
}
],
"departure_datetime": "2022-06-13T19:45:00 02:00",
"arrival_datetime": "2022-06-13T20:55:00 02:00",
"departure_country": "LT",
"arrival_country": "EE",
"departure_city": "VNO",
"arrival_city": "TLL"
}
]
}
]
}
Headers on Postman:
Example endpoint where I call the service:
@ResponseBody
@GetMapping("/abc")
public ResponseEntity<String> postQuotes(){
final String quoteURL = "https://api.xcover.com/x/partners/DCEPN/quotes/";
final HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("X-Api-Key", "5c6a8279bbcee600012561bbf7526e4a1e284c229ae8d22aa512f59f");
headers.add("Date", "Thu, 02 Jun 2022 22:16:39 GMT");
String signatureKeyID = "Signature keyId=\"5c6a8279bbcee600012561bbf7526e4a1e284c229ae8d22aa512f59f\",";
String algoritm = "algorithm=\"hmac-sha1\",";
String signature = "signature=\"zWt0aXNfqzatjxHIbIt+H7iTxLo=\"";
String authHeader = signatureKeyID.concat(algoritm).concat(signature);
headers.add("Authorization", authHeader);
HttpEntity<QuoteRequest> entity = new HttpEntity<>(fillQuoteRequest(), headers);
ResponseEntity<String> postForEntity = rest.postForEntity(quoteURL, entity, String.class);
return postForEntity;
}
Methods where I set Entity objects:
private Legs fillLegs(){
Legs legs = new Legs();
legs.setDepartureDatetime("2022-06-13T19:45:00 02:00");
legs.setArrivalDatetime("2022-06-13T20:55:00 02:00");
legs.setFlightNumber("BT905");
legs.setOperatingAirlineIataCode("BT905");
legs.setMarketingAirlineIataCode("BT905");
legs.setDepartureAirport("VNO");
legs.setArrivalAirport("TLL");
legs.setArrivalCountry("LT");
legs.setDepartureCountry("EE");
return legs;
}
private Flights fillFlights(){
Flights flights = new Flights();
List<Legs> legsList = new ArrayList<>();
legsList.add(fillLegs());
flights.setLegs(legsList);
flights.setDepartureDateTime("2022-06-13T19:45:00 02:00");
flights.setArrivalDateTime("2022-06-13T19:45:00 02:00");
flights.setDepartureCountry("LT");
flights.setArrivalCountry("EE");
flights.setDepartureCity("VNO");
flights.setArrivalCity("TLL");
return flights;
}
private QRequest fillQrequest(){
QRequest qRequest = new QRequest();
List<Flights> flightsList = new ArrayList<>();
flightsList.add(fillFlights());
qRequest.setFlights(flightsList);
qRequest.setPolicyType("comprehensive_travel_insurance");
qRequest.setPolicyTypeVersion("7");
qRequest.setIsReturn(false);
qRequest.setPolicyStartDate("2022-06-07T10:22:30.925568 01:00");
qRequest.setPolicyEndDate("2022-06-13T20:55:00 02:00");
qRequest.setDepartureCountry("LT");
qRequest.setDestinationCountry("EE");
qRequest.setTotalTicketsPrice(78.99f);
qRequest.setNumberOfAdults(1);
qRequest.setNumberOfChildren(0);
qRequest.setNumberOfInfants(0);
qRequest.setTripStartDate("2022-06-13T19:45:00 02:00");
qRequest.setTripEndDate("2022-06-13T20:55:00 02:00");
return qRequest;
}
private PartnerMetadata fillPartnerMeta(){
PartnerMetadata pm = new PartnerMetadata();
pm.setDevice("app");
pm.setSalesChannel("inPath");
return pm;
}
private QuoteRequest fillQuoteRequest(){
QuoteRequest qr = new QuoteRequest();
qr.setRequest(fillQrequest());
qr.setPartnerMetadata(fillPartnerMeta());
qr.setCurrency("EUR");
qr.setCustomerCountry("NL");
qr.setCustomerLanguage("en");
return qr;
}
As a result of all, the error returned when I call the service:
org.springframework.web.client.HttpClientErrorException$UnprocessableEntity: 422 Unprocessable Entity: "{"type":"validation_error","message":"An API error occurred.","errors":{"request":["Expected a list of items but got type \"dict\"."]}}"
Can you help me find the source of the error?
CodePudding user response:
As far as I can see flights
and legs
are supposed to be sent as list, but you are sending them as objects.