restTemplate返回“Count not extract response”异常

时间:2015-03-19 22:51:14

标签: spring rest spring-mvc resttemplate

我有以下代码来使用REST Web服务并转换结果;但是,当我运行代码时它返回以下异常,我也不知道如何处理其他类型的响应,例如,如果响应中包含错误代码它的身体归还了。我发现这些问题12有类似主题,但在那里找不到多少。

异常

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

1 个答案:

答案 0 :(得分:1)

您的异常是因为您尚未注册消息转换器来处理从服务返回的xml响应。你可以使用xstream marshallers等,网上有很多例子。

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

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