无法使用JAXB解析XML消息org.springframework.oxm.UnmarshallingFailureException

时间:2016-01-16 09:14:16

标签: java xml spring jaxb

我使用以下代码来解析soap响应,但我收到UnmarshallingFailureException,我将@XmlSeeAlso更改为@XMLRootElement但问题仍然存在。 WSDL是here

  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}Request>,<{ElsyArres.API}Response>,
       <{ElsyArres.API}SearchFlights>,<{ElsyArres.API}SoapMessage>

代码

   @XmlRootElement(name = "SoapMessage")
   @XmlAccessorType(XmlAccessType.FIELD)
   public class WegoloSoapMessageResponse {
       @XmlElement(name = "Username")
       private String username;
       @XmlElement(name = "Password")
       private String password;
       @XmlElement(name = "LanguageCode")
       private String languageCode;
       @XmlElement(name = "ErrorMessage")
       private String errorMessage;
       @XmlElement(name = "ErrorCode")
       private int errorCode;
       @XmlElement(name = "AppVersion")
       private String appVersion;
       @XmlElement(name = "Request")
       private Request request;
       @XmlElement(name = "Response")
       private Response response;

       getters and setters


   @XmlRootElement(name = "Request")
   @XmlAccessorType(XmlAccessType.FIELD)
   public class Request {
       @XmlElement(name = "Departure")
       private String departure;
       @XmlElement(name = "Destination")
       private String destination;
       @XmlElement(name = "DepartureDate")
       private String departureDate;
       @XmlElement(name = "ReturnDate")
       private String returnDate;
       @XmlElement(name = "NumADT")
       private int numADT;
       @XmlElement(name = "NumINF")
       private int numInf;
       @XmlElement(name = "NumCHD")
       private int numCHD;
       @XmlElement(name = "CurrencyCode")
       private String currencyCode;
       @XmlElement(name = "WaitForResult")
       private boolean waitForResult;
       @XmlElement(name = "NearByDepartures")
       private boolean nearByDepartures;
       @XmlElement(name = "NearByDestinations")
       private boolean nearByDestinations;
       @XmlElement(name = "RROnly")
       private boolean rronly;
       @XmlElement(name = "MetaSearch")
       private boolean metaSearch;

       getters and setters


   @XmlRootElement(name="Response")
   @XmlAccessorType(XmlAccessType.FIELD)
   public class Response {
       @XmlElement(name="SearchFlightId")
       private String searchFlightId;
       @XmlElement(name="Roundtrip")
       private boolean roundTrip;
       @XmlElement(name="CurrencyCode")
       private String currencyCode;
       @XmlElement(name="Flights")
       private Flights flights;

       getters and setters



    @XmlSeeAlso(Flight.class)
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Flights {
       @XmlElement(name="Flight")
       private List<Flight> flight;

       getter and setter



   @XmlSeeAlso(Outbound.class)
   @XmlAccessorType(XmlAccessType.FIELD)
   public class Flight {

       @XmlElement(name = "Outbound")
       private Outbound outbound;
       @XmlElement(name="Inbound")
       private Inbound inbound;
       @XmlElement(name = "BagFee")
       private int bagFee;
       @XmlElement(name = "CcFee")
       private int ccFee;
       @XmlElement(name = "HandlingFee")
       private int handlingFee;
       @XmlElement(name = "TotalFare")
       private int totalFare;
       @XmlElement(name = "FlightId")
       private String flightId;
       @XmlElement(name = "Link2Book")
       private String link2Book;
       @XmlElement(name = "Provider")
       private String provider;

       getters and setters




   @XmlRootElement(name = "Outbound")
   @XmlAccessorType(XmlAccessType.FIELD)
   public class Outbound {

       @XmlElement(name="CarName")
       private String carName;
       @XmlElement(name="CarCode")
       private String carCode;
       @XmlElement(name="DepName")
       private String depName;
       @XmlElement(name="DepCode")
       private String depCode;
       @XmlElement(name="DestName")
       private String destName;
       @XmlElement(name="DestCode")
       private String destCode;
       @XmlElement(name="Duration")
       private String duration;
       @XmlElement(name="FlightNo")
       private String flightNo;
       @XmlElement(name="DepDateTime")
       private Date dapDateTime;
       @XmlElement(name="ArrDateTime")
       private Date arrDateTime;
       @XmlElement(name="Legs")
       private Legs legs;
       @XmlElement(name="Taxes")
       private int taxes;
       @XmlElement(name="FareADT")
       private int fareADT;
       @XmlElement(name="FareCHD")
       private int fareCHD;
       @XmlElement(name="FareINF")
       private int fareInf;
       @XmlElement(name="MiscFees")
       private int miscFees;
       @XmlElement(name="Idx")
       private int idx;
       @XmlElement(name="FareClass")
       private String fareClass;
       @XmlElement(name="FareType")
       private String fareType;
       @XmlElement(name="FareId")
       private String fareId;

       getters and setters



   @XmlRootElement(name="Legs")
   @XmlAccessorType(XmlAccessType.FIELD)
   public class Legs {
       @XmlElement(name="Leg")
       private Leg leg;

       getter and setter



   @XmlRootElement(name="Leg")
   @XmlAccessorType(XmlAccessType.FIELD)
   public class Leg {
       @XmlElement(name="Sequence")
       private int sequence;
       @XmlElement(name="FlightNo")
       private String flightNo;
       @XmlElement(name="DepCode")
       private String depCode;
       @XmlElement(name="DepName")
       private String depName;
       @XmlElement(name="DestCode")
       private String destCode;
       @XmlElement(name="DestName")
       private String destName;
       @XmlElement(name="DepTime")
       private String depTime;
       @XmlElement(name="ArrTime")
       private String arrTime;
       @XmlElement(name="CarCode")
       private String carCode;
       @XmlElement(name="CarName")
       private String carName;
       @XmlElement(name="FareClass")
       private String fareClass;
       @XmlElement(name="ArrDateTime")
       private Date arrDateTime;
       @XmlElement(name="DepDateTime")
       private Date depDateTime;

       getters and setters



    @XmlRootElement(name = "Inbound")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Inbound {
        @XmlElement(name="CarName")
        private String carName;
        @XmlElement(name="CarCode")
        private String carCode;
        @XmlElement(name="DepName")
        private String depName;
        @XmlElement(name="DepCode")
        private String depCode;
        @XmlElement(name="DestName")
        private String destName;
        @XmlElement(name="DestCode")
        private String destCode;
        @XmlElement(name="Duration")
        private String duration;
        @XmlElement(name="FlightNo")
        private String flightNo;
        @XmlElement(name="DepDateTime")
        private Date dapDateTime;
        @XmlElement(name="ArrDateTime")
        private Date arrDateTime;
        @XmlElement(name="Legs")
        private Legs legs;
        @XmlElement(name="Taxes")
        private int taxes;
        @XmlElement(name="FareADT")
        private int fareADT;
        @XmlElement(name="FareCHD")
        private int fareCHD;
        @XmlElement(name="FareINF")
        private int fareInf;
        @XmlElement(name="MiscFees")
        private int miscFees;
        @XmlElement(name="Idx")
        private int idx;
        @XmlElement(name="FareClass")
        private String fareClass;
        @XmlElement(name="FareType")
        private String fareType;
        @XmlElement(name="FareId")
        private String fareId;

        gettes and setters

jaxb.in​​dex

SearchFlights
Flight
Flights
Leg
Legs
Outbound
Request
Response
WegoloSoapMessage

package-info.java

@XmlSchema( 
    namespace = "ElsyArres.API",
    elementFormDefault = XmlNsForm.QUALIFIED) 
package com.myproject.flights.wegolo;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

响应

1

enter image description here

2

enter image description here

3

enter image description here

4

enter image description here

5

enter image description here

6

enter image description here

3 个答案:

答案 0 :(得分:3)

错误消息指出,您正在尝试解析未在类中映射的本地节点searchflightsresponse作为注释。

您必须声明此节点,因为您之前已经使用了另一个节点。

请注意,在这里使用jaxb解析soap响应是不可取的,因为结构(如您所见)并非无足轻重。我建议下载wsdl并生成客户端存根以匹配webservice。然后,您不需要注释数百个成员和类。

另见: How to generate java classes from WSDL file

答案 1 :(得分:1)

您似乎手动创建bean或至少正在编辑它们。 应该避免这两种情况。相反,您应该尝试使用可用的工具为您生成Jaxb类和Soap客户端。两个非常常见的选项是spring webservices或众所周知的Apache CXF framework

答案 2 :(得分:1)

您需要使用wsdl来生成响应xml。 构建响应xml,也是如此之大,必然会缺少元素。

这是一个可以用来开始的教程,

http://www.mkyong.com/webservices/jax-ws/jax-ws-wsgen-tool-example/ http://www.mkyong.com/webservices/jax-ws/jax-ws-hello-world-example-document-style/