I need to call a REST service from springboot with GET operation. The service is for a third party vendor, so I don't have control over it. They sent me a sample of the input as an xml file and schema with the object names for the request and response. I generated the java classes from the schema using xjc. The service i/o is XML not Json.
The service takes an object as a param that contains 5 fields similar to this:
@XmlRootElement(name = "rateRequest")
public class RateRequest {
@XmlElement(required = true)
protected String shipPoint;
@XmlElement(required = true)
protected String postalCode;
@XmlElement(required = true)
protected String customerNbr;
@XmlElement(required = true)
protected String countryCode;
}
the service url is something like "http:..../GetRate"
I am not sure how to pass the object above as an input and call the service. I am using springboot , I tried this:
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
// set `Content-Type` and `Accept` headers
headers.setContentType(MediaType.APPLICATION_XML);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_XML));
HttpEntity<RateResponse> httpRequest = new HttpEntity<RateResponse>(headers);
ResponseEntity<RateResponse> response = restTemplate.exchange(serviceEp, HttpMethod.GET, httpRequest, RateResponse.class);
But I am not sure how to pass the object as an input in the httpRequest.
Do I need to add every field in the object as a query param to the httpRequest or do I need to marshall the object to xml string and pass the string as body? (I am not sure how to do that either).
The only thing the vendor sent me was an xml file like this (as an example of the input):
<RateRequest>
<shipPoint>20</shipPoint>
<customerNbr>12345</customerNbr>
<postalCode>23456</postalCode>
<countryCode>US</countryCode>
</RateRequest>