UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException

牧云@^-^@ 提交于 2019-12-13 06:05:11

问题


I am trying to send a SOAP request and receive the response by WebServiceTemplate but I am receiving following exception:

The WSDL is here.

SEVERE: Servlet.service() for servlet [bb] in context with path [/Bb] threw 
exception [Request processing failed; nested exception is 
org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling 
exception; nested exception is javax.xml.bind.UnmarshalException: unexpected 
element (uri:"http://ticketmaster.productserve.com/v2/soap.php", 
local:"findEventsResponse"). Expected elements are <{}findEvents>,<{}request>,
<{}sortTicket>] with root cause

Request

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({Request.class})
public class FindEvents {
    @XmlElement
    private Request request;

           getter / setter
}


@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({SortTicket.class})
public class Request {
    @XmlElement
    private String apiKey;
    @XmlElement
    private String country;
    @XmlElement
    private int resultsPerPage;
    @XmlElement
    private int currentPage;
    @XmlElement(name = "Sort")
    private SortTicket sort;
    @XmlElement
    private String[] filters;
    @XmlElement
    private String updatedSince;

           getters / setters
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SortTicket {
    @XmlElement
    private String field;
    @XmlElement
    private String order;

           getters / setters
}

Response

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({Return.class})
public class FindEventsResponse {
    @XmlElement
    private Return returnResult;

           getters / setters
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({Details.class,Results.class})
public class Return {
    @XmlElement
    private Details details;
    @XmlElement
    private Results results;

           getters / setters
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Details {
    @XmlElement
    private int totalResults;
    @XmlElement
    private int totalPages;
    @XmlElement
    private int currentPage;
    @XmlElement
    private int resultsPerPage;

    getters / setters
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({ Artist.class, Venue.class })
public class Results {
    @XmlElement
    private long eventId;
    @XmlElement
    private String ticketmasterEventId;
    @XmlElement
    private String status;
    @XmlElement
    private String name;
    @XmlElement
    private String url;
    @XmlElement
    private String eventDate;
    @XmlElement
    private String onSaleDate;
    @XmlElement
    private String preSaleDate;
    @XmlElement
    private int categoyId;
    @XmlElement
    private String parentCategory;
    @XmlElement
    private int parentCategoryId;
    @XmlElement
    private Double minPrice;
    @XmlElement
    private Double maxPrice;
    @XmlElement(name = "Artists")
    private Artist artist;
    @XmlElement
    private Venue venue;

     getters / setters
}


@XmlRootElement(name = "Artists")
@XmlAccessorType(XmlAccessType.FIELD)
public class Artist {
    @XmlElement
    private long artistId;
    @XmlElement
    private long ticketMasterArtistId;
    @XmlElement
    private String name;
    @XmlElement
    private String url;
    @XmlElement
    private String imageUrl;
    @XmlElement
    private String category;
    @XmlElement
    private int categoryId;
    @XmlElement
    private String parentCategory;
    @XmlElement
    private int parentCategoryId;

           getters / setters
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Venue {
    @XmlElement
    private long venueId;
    @XmlElement
    private long ticketmasterVenueId;
    @XmlElement
    private String name;
    @XmlElement
    private String street;
    @XmlElement
    private String city;
    @XmlElement
    private String country;
    @XmlElement
    private String postcode;
    @XmlElement
    private String url;
    @XmlElement
    private String imageUrl;
    @XmlElement
    private String state;

    getters / setters
}

Configuration is as follows

<oxm:jaxb2-marshaller id="marshaller">
        <oxm:class-to-be-bound name="com.bb.tickets.ticketmaster.FindEvents"/>
    </oxm:jaxb2-marshaller>

    <bean id="webServiceTemplate" 
        class="org.springframework.ws.client.core.WebServiceTemplate">
        <property name="marshaller" ref="marshaller" />
        <property name="unmarshaller" ref="marshaller" />
        <property name="defaultUri"
            value="http://ticketmaster.productserve.com/v2/soap.php" />

    </bean>

The code to send the request and receive response

SortTicket sortTicket = new SortTicket();
Request request = new Request();
request.setApiKey("APIKEY");
request.setCountry("UK");
FindEvents findEvents = new FindEvents();
findEvents.setRequest(request);
FindEventsResponse response = (FindEventsResponse) 
webServiceTemplate.marshalSendAndReceive(findEvents);
System.err.println("response:"+ 
response.getReturnResult().getDetails().getCurrentPage());  

回答1:


From the stack trace, I understand that JAX-B received a findEventsResponse of type FindEventsResponse to marshall but it expected a FindEvents object.

The issue lies within your spring configuration. You only told Spring that the FindEvents class should be bound to your marshaller. You need to add the FindEventsResponse class too, using the classesToBeBound property :

<oxm:jaxb2-marshaller id="marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.bb.tickets.ticketmaster.FindEvents</value>
            <value>com.bb.tickets.ticketmaster.FindEventsResponse</value>
        </list>
    </property>
</oxm:jaxb2-marshaller>

You could also bound a package using the packageToScan property, check this question.

Hope that does it.

[Edit]: if it doesn't work simply try :

<oxm:jaxb2-marshaller id="marshaller">
    <oxm:class-to-be-bound name="com.bb.tickets.ticketmaster.FindEvents"/>
    <oxm:class-to-be-bound name="com.bb.tickets.ticketmaster.FindEventsResponse"/>
</oxm:jaxb2-marshaller>


来源:https://stackoverflow.com/questions/32181738/unmarshallingfailureexception-jaxb-unmarshalling-exception-nested-exception-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!