I have following code to consume a REST webservice and convert the results;however, when I run the code it returns following exception, I am also not sure how to handle other type of responses for example if a response with error code in its body is returned. I have found this questions 1 and 2 with similar topics but did not find much there.
Exception
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.project.web.FlightsResults] and content type [application/xml]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:110)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:576)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:537)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:244)
at com.project.web.ArticleController.showArticles(ArticleController.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
....
The rest template code is as following:
RestTemplate restTemplate = new RestTemplate();
Map<String, String> vars = new HashMap<String, String>();
vars.put("user", "username");
vars.put("key", "password");
vars.put("fl", "po");
AvailabilityResponse flightResults = restTemplate
.getForObject(
"http://ift.tt/197CSYx}",
AvailabilityResponse.class, vars);
System.err.println(">>"
+ flightResults.getFlightList().get(0).getFlightOptions()
.getFlightOption().size());
XMLElements
@XmlRootElement(name = "availabilityResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class AvailabilityResponse {
@XmlElement(name = "flightList")
private List<FlightList> flightList;
public AvailabilityResponse() {
this.flightList = new ArrayList();
}
public List<FlightList> getFlightList() {
return flightList;
}
public void setFlightList(List<FlightList> flightList) {
this.flightList = flightList;
}
}
@XmlRootElement(name = "flightList")
@XmlAccessorType(XmlAccessType.FIELD)
public class FlightList {
@XmlElement(name = "flightOptions")
private FlightOptions flightOptions;
public FlightOptions getFlightOptions() {
return flightOptions;
}
public void setFlightOptions(FlightOptions flightOptions) {
this.flightOptions = flightOptions;
}
}
@XmlRootElement(name = "flightOptions")
@XmlAccessorType(XmlAccessType.FIELD)
public class FlightOptions {
@XmlElement(name = "flightOption")
private List<FlightOption> flightOption;
public FlightOptions() {
this.flightOption = new ArrayList();
}
public List<FlightOption> getFlightOption() {
return flightOption;
}
public void setFlightOption(List<FlightOption> flightOption) {
this.flightOption = flightOption;
}
}
@XmlRootElement(name = "flightOption")
@XmlAccessorType(XmlAccessType.FIELD)
public class FlightOption {
@XmlElement(name = "viaIata")
private String viaIata;
@XmlElement(name = "fromDate")
private String fromDate;
@XmlElement(name = "toDate")
private String toDate;
@XmlElement(name = "fromTime")
private String fromTime;
@XmlElement(name = "toTime")
private String toTime;
@XmlElement(name = "flightNum")
private String flightNum;
@XmlElement(name = "class")
private String fclass;
@XmlElement(name = "flightlegs")
private List<FlightLeg> flightLegs;
@XmlElement(name = "prices")
private Prices prices;
public FlightOption() {
this.flightLegs = new ArrayList();
this.prices = new Prices();
}
getters and setters
@XmlRootElement (name = "prices")
@XmlAccessorType (XmlAccessType.FIELD)
public class Prices {
@XmlElement (name ="adult")
private float adult;
@XmlElement (name ="child")
private float child;
@XmlElement (name = "infant")
private float infant;
@XmlElement (name = "total")
private Total total;
getters and setters
@XmlRootElement (name = "total")
@XmlAccessorType (XmlAccessType.FIELD)
public class Total {
@XmlAttribute (name ="serviceCharge")
private float serviceCharge;
@XmlAttribute (name = "taxCharge")
private float taxCharge;
@XmlAttribute (name ="taxGeneral")
private float taxGeneral;
@XmlAttribute (name = "totalPrice")
private float totalPrice;
@XmlAttribute (name ="currency")
private String currency;
getters and setters
REST response
<availabilityResponse version="3">
<flightList fromIata="FRA" toIata="YYZ" flightsFound="8">
<flightOptions>
<flightOption>
<viaIata>FRA-YHZ-YYZ</viaIata>
<fromDate>2015-06-06</fromDate>
<fromTime>13:15:00</fromTime>
<toDate>2015-06-06</toDate>
<toTime>20:10:00</toTime>
<flightNum>DEA062</flightNum>
<class>C</class>
<flightlegs>
<flightlegdetail fromIata="FRA" toIata="YHZ">
<fromDate>2015-06-06</fromDate>
<fromTime>13:15:00</fromTime>
<toDate>2015-06-06</toDate>
<toTime>15:35:00</toTime>
<flightNum>DE6062</flightNum>
</flightlegdetail>
</flightlegs>
<flightlegs>
<flightlegdetail fromIata="YHZ" toIata="YYZ">
<fromDate>2015-06-06</fromDate>
<fromTime>18:50:00</fromTime>
<toDate>2015-06-06</toDate>
<toTime>20:10:00</toTime>
<flightNum>WS269</flightNum>
</flightlegdetail>
</flightlegs>
<prices currency="EUR" specialOffer="true">
<adult>724.22</adult>
<child>725.00</child>
<infant>73.00</infant>
<total serviceCharge="0.00" taxCharge="85.77" taxGeneral="85.77"
flightPrice="724.22" totalPrice="809.99" currency="EUR" />
</prices>
</flightOption>
<flightOption>
<viaIata>FRA-YYZ</viaIata>
.....
Aucun commentaire:
Enregistrer un commentaire