restTemplate returns "Count not extract response" 异常

restTemplate returns "Count not extract response" exception

我有以下代码来使用 REST 网络服务并转换结果;但是,当我 运行 代码 returns 出现异常时,我也不确定如何处理其他类型的responses 例如,如果返回正文中包含错误代码的响应。我发现这个问题 and 2 具有相似的主题,但没有找到太多。

异常

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)
....

其余模板代码如下:

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://example.com/availabilityRequest?user={user}&key={key}&fl_type={fl}",
                        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 响应

  <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>
                 .....

您的异常是因为您没有注册消息转换器来处理从服务返回的 xml 响应。您可以使用 xstream 编组器等,网络上有很多示例。

http://www.informit.com/guides/content.aspx?g=java&seqNum=546

https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate