无法解析来自WSDL的生成存根的响应

时间:2016-02-07 10:10:07

标签: spring web-services jaxb wsdl

我用Maven生成了JAXB文件。 WSDL是here。我使用过滤器为名称冲突应用编号。

运行代码后,会显示以下错误。

[com.sun.istack.SAXException2: unable to marshal type 
"com.myproject.flight.SearchFlights2" as an element because it is missing an 
@XmlRootElement annotation]

@XmlRootElement注释添加到SearchFlights2类后,代码会显示以下错误!

Caused by: javax.xml.bind.UnmarshalException: unexpected element 
(uri:"ElsyArres.API", local:"SearchFlightsResponse"). Expected elements are 
<{ElsyArres.API}Inbound>,<{ElsyArres.API}Leg>,<{ElsyArres.API}Legs>,
<{ElsyArres.API}Outbound>,<{ElsyArres.API}searchFlights2>

我搜索了Legs这个词,唯一一个Legs是FlightInfo的文件,但它没有Inbound,Outbound等。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "FlightInfo", propOrder = {
    "carName",
    "carCode",
    "depName",
    "depCode",
    "destName",
    "destCode",
    "duration",
    "flightNo",
    "depDateTime",
    "arrDateTime",
    "legs",
    "taxes",
    "fareADT",
    "fareCHD",
    "fareINF",
    "miscFees",
    "idx",
    "fareClass",
    "fareType",
    "fareId"
})
@XmlSeeAlso({
    FlightDetail.class,
    OnewayFlight.class
})
public class FlightInfo {

    @XmlElement(name = "CarName")
    protected String carName;
    @XmlElement(name = "CarCode")
    protected String carCode;
    @XmlElement(name = "DepName")
    protected String depName;
    @XmlElement(name = "DepCode")
    protected String depCode;
    @XmlElement(name = "DestName")
    protected String destName;
    @XmlElement(name = "DestCode")
    protected String destCode;
    @XmlElement(name = "Duration")
    protected String duration;
    @XmlElement(name = "FlightNo")
    protected String flightNo;
    @XmlElement(name = "DepDateTime", required = true)
    @XmlSchemaType(name = "dateTime")
    protected XMLGregorianCalendar depDateTime;
    @XmlElement(name = "ArrDateTime", required = true)
    @XmlSchemaType(name = "dateTime")
    protected XMLGregorianCalendar arrDateTime;
    @XmlElement(name = "Legs")
    protected ArrayOfLeg legs;
    @XmlElement(name = "Taxes")
    protected long taxes;
    @XmlElement(name = "FareADT")
    protected long fareADT;
    @XmlElement(name = "FareCHD")
    protected long fareCHD;
    @XmlElement(name = "FareINF")
    protected long fareINF;
    @XmlElement(name = "MiscFees")
    protected long miscFees;
    @XmlElement(name = "Idx")
    protected long idx;
    @XmlElement(name = "FareClass", required = true)
    @XmlSchemaType(name = "string")
    protected FareClass fareClass;
    @XmlElement(name = "FareType", required = true)
    @XmlSchemaType(name = "string")
    protected FareType fareType;
    @XmlElement(name = "FareId")
    protected String fareId;

客户端

SearchFlightsResponse response = (SearchFlightsResponse) webServiceTemplate
                    .marshalSendAndReceive(
                            "http://www5v80.elsyarres.net/service.asmx",
                            searchFlights, new WebServiceMessageCallback() {
                                public void doWithMessage(
                                        WebServiceMessage message) {
                                    ((SoapMessage) message)
                                            .setSoapAction("ElsyArres.API/SearchFlights");
                                }
                            });

            SearchFlightsResponse msg = (SearchFlightsResponse) response;
            System.err.println("Wegolo >>>"
                    + msg.getSoapMessage().getResponse().getFlights()
                            .getFlight().size());

JAXB

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "soapMessage" })
@XmlRootElement(name = "SearchFlightsResponse")
public class SearchFlightsResponse {

    @XmlElement(name = "SoapMessage")
    protected SearchFlights2 soapMessage;

    getters and setters



@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SearchFlights", propOrder = { "request", "response" })
public class SearchFlights2 extends SoapMessageBase {

    @XmlElement(name = "Request")
    protected SearchFlightsRequest request;
    @XmlElement(name = "Response")
    protected SearchFlightsResponse2 response;

    getters and setters


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SearchFlightsRequest", propOrder = { "departure",
        "destination", "departureDate", "returnDate", "numADT", "numINF",
        "numCHD", "currencyCode", "customContainer", "carrierList",
        "fareClasses", "providers", "waitForResult", "nearbyDepartures",
        "nearbyDestinations", "rrOnly", "metaSearch" })
public class SearchFlightsRequest {

    @XmlElement(name = "Departure")
    protected String departure;
    @XmlElement(name = "Destination")
    protected String destination;
    @XmlElement(name = "DepartureDate")
    protected String departureDate;
    @XmlElement(name = "ReturnDate")
    protected String returnDate;
    @XmlElement(name = "NumADT")
    protected int numADT;
    @XmlElement(name = "NumINF")
    protected int numINF;
    @XmlElement(name = "NumCHD")
    protected int numCHD;
    @XmlElement(name = "CurrencyCode", required = true)
    @XmlSchemaType(name = "string")
    protected CurrencyCode currencyCode;
    @XmlElement(name = "CustomContainer")
    protected String customContainer;
    @XmlElement(name = "CarrierList")
    protected ArrayOfString carrierList;
    @XmlElement(name = "FareClasses")
    protected ArrayOfFareClass fareClasses;
    @XmlElement(name = "Providers")
    protected ArrayOfProvider providers;
    @XmlElement(name = "WaitForResult")
    protected boolean waitForResult;
    @XmlElement(name = "NearbyDepartures")
    protected boolean nearbyDepartures;
    @XmlElement(name = "NearbyDestinations")
    protected boolean nearbyDestinations;
    @XmlElement(name = "RROnly")
    protected boolean rrOnly;
    @XmlElement(name = "MetaSearch")
    protected boolean metaSearch;

    getters and setters


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SearchFlightsResponse", propOrder = {
    "searchFlightId",
    "roundtrip",
    "currencyCode",
    "flights"
})
public class SearchFlightsResponse2 {

    @XmlElement(name = "SearchFlightId")
    protected String searchFlightId;
    @XmlElement(name = "Roundtrip")
    protected boolean roundtrip;
    @XmlElement(name = "CurrencyCode", required = true)
    @XmlSchemaType(name = "string")
    protected CurrencyCode currencyCode;
    @XmlElement(name = "Flights")
    protected ArrayOfFlight flights;

    getters and setters

的Maven

<plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.12.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <args>
                        <arg>-XautoNameResolution</arg>
                    </args>

                    <schemaLanguage>WSDL</schemaLanguage>
                    <generatePackage>hello.wsdl</generatePackage>
                    <schemas>
                        <schema>
                            <url>http://www5v80.elsyarres.net/service.asmx?wsdl</url>
                        </schema>
                    </schemas>
                </configuration>
            </plugin>

1 个答案:

答案 0 :(得分:1)

您需要使用以下

的绑定
<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings wsdlLocation="http://www5v80.elsyarres.net/service.asmx?wsdl"
            xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
            xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
            xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" version="2.1"
            xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <jaxws:bindings node="wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='ElsyArres.API']">
        <jaxb:schemaBindings>
            <jaxb:nameXmlTransform>
                <jaxb:elementName suffix="Elem"/>
            </jaxb:nameXmlTransform>
        </jaxb:schemaBindings>
    </jaxws:bindings>
</jaxws:bindings>

你的maven应该使用以下插件

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>2.4.1</version>
    <executions>
        <execution>
            <goals>
                <goal>wsimport</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <bindingFiles>
         <bindingFile>${basedir}/WebContent/resources/bindings.xjb</bindingFile>
        </bindingFiles>
        <wsdlUrls>
            <wsdlUrl>http://www5v80.elsyarres.net/service.asmx?wsdl</wsdlUrl>
        </wsdlUrls>
        <vmArgs>
            <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg>
        </vmArgs>
    </configuration>
</plugin>

请务必使用SearchFlightsElem代表请求,SearchFlightsResponseElem代表回复。