无法使用 JAXB 解组收到的请求

cannot unmarshal received request using JAXB

我定义了所有必需的元素,但代码无法解析服务器响应。我将 @XmlSeeAlso({ Artist.class, Venue.class }) 更改为 @XmlSeeAlso({Venue.class }) 但它没有帮助。

我还添加了一个 package-info.java 文件。

@XmlSchema(
    namespace = "http://ticketmaster.productserve.com/v2/soap.php",
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
)
package com.myproject.tickets.ticketmaster;

import javax.xml.bind.annotation.*;

错误

org.springframework.http.converter.HttpMessageNotReadableException: Could not 
unmarshal to [class com.myproject.tickets.ticketmaster.Data]: unexpected 
element (uri:"", local:"data"). Expected elements are 
 <{http://ticketmaster.productserve.com/v2/soap.php}artist>, 
 <{http://ticketmaster.productserve.com/v2/soap.php}data>,
 <{http://ticketmaster.productserve.com/v2/soap.php}details>,
 <{http://ticketmaster.productserve.com/v2/soap.php}results>,
 <{http://ticketmaster.productserve.com/v2/soap.php}venue>; nested exception 
  is javax.xml.bind.UnmarshalException: unexpected element (uri:"", 
  local:"data"). Expected elements are 
  <{http://ticketmaster.productserve.com/v2/soap.php}artist>,
  <{http://ticketmaster.productserve.com/v2/soap.php}data>,
  <{http://ticketmaster.productserve.com/v2/soap.php}details>,
  <{http://ticketmaster.productserve.com/v2/soap.php}results>,
  <{http://ticketmaster.productserve.com/v2/soap.php}venue>

回应

   <?xml version="1.0"?>    
   <data>
   <details>
      <totalResults></totalResults>
      <totalPages></totalPages>
      <currentPage></currentPage>
      <resultsPerPage></resultsPerPage>
   </details>
   <results>
       <eventId></eventId>  
       <ticketmasterEventId></ticketmasterEventId>   
       <status></status>
       <name></name>
       <url></url>
       <eventDate></eventDate>
       <onSaleDate></onSaleDate>
       <preSaleDate></preSaleDate>
       <category></category>
       <categoryId></categoryId>
       <parentCategory></parentCategory>
       <parentCategoryId></parentCategoryId>
       <minPrice></minPrice>
       <maxPrice></maxPrice>
       <artists>
          <artistId></artistId>
          <ticketmasterArtistId></ticketmasterArtistId>
          <name></name>
          <url></url>
          <imageUrl></imageUrl>
          <category></category>
          <categoryId></categoryId>
          <parentCategory></parentCategory>       
          <parentCategoryId></parentCategoryId>
       </artists>
       <venue>
          <venueId></venueId>
          <ticketmasterVenueId></ticketmasterVenueId>
          <name></name>
          <street></street>
          <city></city>
          <country></country>
          <postcode></postcode>
          <url></url>
          <imageUrl></imageUrl>
          <state/>
        </venue>
    </results>`    
    <results>
       .....
    </results>
    </data>

数据

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Data {
    @XmlElement
    Details details;
    @XmlElement
    List<Results> results;

    public Data() {
        super();
        this.results = new ArrayList<Results>();
    }

详情

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

结果

@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 List<Artist> artists;
    @XmlElement
    private Venue venue;

    public Results() {
        super();
        this.artists = new ArrayList<Artist>();
    }

艺术家

@XmlRootElement
@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;

你的问题是你的解组器需要一个有命名空间的元素,而你的 XML 没有。

为了解决你的问题,要么:

  • 在您的 XML 中放置一个命名空间(根元素中的 xmlns="http://ticketmaster.productserve.com/v2/soap.php" 属性)

  • 从您的@XmlSchema 注释中删除名称空间属性