XML解析:MismatchedInputException:无法构造`A`的实例(尽管至少存在一个Creator)

时间:2019-09-16 17:23:59

标签: java xml-parsing jackson mpeg-dash

我正在使用third-party library来解析清单的MPD(用于DASH视频流),但是在解析过程中,在将XML解析为对象时遇到了MismatchedInputException。清单可以在this URL(是实时流式清单)中找到。

显示的错误是随后的错误:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `io.lindstrom.mpd.data.descriptor.Scope` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (StringReader); line: 17, column: 5] (through reference chain: io.lindstrom.mpd.data.MPD["ServiceDescription"]->io.lindstrom.mpd.data.descriptor.ServiceDescription["Scope"])
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63) ~[jackson-databind-2.9.0.jar:2.9.0]
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1329) ~[jackson-databind-2.9.0.jar:2.9.0]

现在,一些上下文。

在同一清单中,“ ServiceDescription”标记看起来像这样(我无法更改)

  <ServiceDescription id="1">
    <Scope
      schemeIdUri="urn:dvb:dash:lowlatency:scope:2019" />
    <Latency
      target="2000"
      max="6000"
      min="1000" />
    <PlaybackRate
      max="1.5"
      min="0.5" />
  </ServiceDescription>

范围代码是这样定义的:

import javax.xml.bind.annotation.XmlAttribute;

public class Scope {

  @XmlAttribute(name = "schemeIdUri")
  private String schemeIdUri;

  public Scope(String schemeIdUri) {
    this.schemeIdUri = schemeIdUri;
  }

  public String getSchemeIdUri() {
    return schemeIdUri;
  }

  public void setSchemeIdUri(String schemeIdUri) {
    this.schemeIdUri = schemeIdUri;
  }
}

包含“范围”的类的定义如下。我将定义它的标签更改为@ XmlElement,@ XmlAttribute,它不会给我任何不同的结果:

package io.lindstrom.mpd.data.descriptor;

import java.util.Objects;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

@XmlType(propOrder = {
    "id",
    "scope",
    "any",
    "any"
})

public class ServiceDescription {

  @XmlAttribute(name = "id")
  private String id;

  @XmlAttribute(name = "Scope")
  private Scope scope;

  @XmlAttribute(name = "Latency")
  private Latency latency;

  @XmlAttribute(name = "PlaybackRate")
  private PlaybackRate playbackRate;

  public ServiceDescription(String id, Scope scope, Latency latency,
      PlaybackRate playbackRate) {
    this.id = id;
    this.scope = scope;
    this.latency = latency;
    this.playbackRate = playbackRate;
  }

  public ServiceDescription() {
    this.id = null;
    this.scope = null;
    this.latency = null;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public Scope getScope() {
    return scope;
  }

  public void setScope(Scope scope) {
    this.scope = scope;
  }

  public Latency getLatency() {
    return latency;
  }

  public void setLatency(Latency latency) {
    this.latency = latency;
  }

  public PlaybackRate getPlaybackRate() {
    return playbackRate;
  }

  public void setPlaybackRate(PlaybackRate playbackRate) {
    this.playbackRate = playbackRate;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    ServiceDescription that = (ServiceDescription) o;
    return id == that.id &&
        scope.equals(that.scope) &&
        latency.equals(that.latency) &&
        playbackRate.equals(that.playbackRate);
  }

  @Override
  public int hashCode() {
    return Objects.hash(id, scope, latency, playbackRate);
  }

  public final class Builder {

    private String id;
    private Scope scope;
    private Latency latency;
    private PlaybackRate playbackRate;

    public Builder withId(String id) {
      this.id = id;
      return this;
    }

    public Builder withScope(Scope scope) {
      this.scope = scope;
      return this;
    }

    public Builder withLatency(Latency latency) {
      this.latency = latency;
      return this;
    }

    public Builder withPlaybackRate(PlaybackRate playbackRate) {
      this.playbackRate = playbackRate;
      return this;
    }

    public ServiceDescription build() {
      ServiceDescription serviceDescription = new ServiceDescription();
      serviceDescription.setId(id);
      serviceDescription.setScope(scope);
      serviceDescription.setLatency(latency);
      serviceDescription.setPlaybackRate(playbackRate);
      return serviceDescription;
    }
  }

}

1 个答案:

答案 0 :(得分:0)

好吧,在some reading之后,并测试了不同的方法,修复该错误所需的唯一事情就是为其添加默认构造函数。

这是Scope类的添加文本

  @SuppressWarnings("unused")
  public Scope(){
    schemeIdUri = null;
  }

有了这个,足以阻止它继续发生。可能它首先创建了空对象,然后添加了属性。