1

特定のインターフェイスのインスタンスの配列を返さなければならない小さなプロジェクトに取り組んでいます。インターフェイスの単一のインスタンスを返すと、すべて正常に動作します。インスタンスの配列を返すと、次のエラーが発生します。


編集 2013 年 8 月 2 日
抽象クラスを使用すると、同じコードが完全に機能します。インターフェースが問題のようです。


Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of             IllegalAnnotationExceptions
Person is an interface and JAXB cannot handle interfaces 
       est une interface et JAXB ne peut pas gérer les interfaces.
    this problem is related to the following location:
    at Person
Person does not have a default empty constructor 
       ne comporte aucun constructeur sans argument par défaut.
    this problem is related to the following location:
    at Person

JAXB および JAX-RS を使用してインターフェースのインスタンスの配列を返す処理を行うにはどうすればよいですか?

私は Gassfish 4 を使用しており、次のことを試しました。データコード:

@XmlRootElement
public interface Person {
  @XmlAttribute
  public String getId();
  @XmlElement
  public String getName();
}

@XmlRootElement
public class P1 implements Person {

  @Override
  @XmlAttribute
  public String getId() {
    return "1";
  }

  @Override
  @XmlElement
  public String getName() {
    return this.getClass().getSimpleName();
  }
}

@XmlRootElement
public class P2 implements Person {

  @Override
  @XmlAttribute
  public String getId() {
    return "2";
  }

  @Override
  @XmlElement
  public String getName() {
    return this.getClass().getSimpleName();
  }
}

JAX-RS コード:

Person[] persons = {new P1(), new P2()};

@GET
@Path("/query")
@Produces({ MediaType.APPLICATION_JSON })
public Person[] findByQuery(@QueryParam("name") String name) {
  return persons;
}

ありがとう!

4

1 に答える 1

1

次のように、インターフェイス クラスから実装クラスを指定してみてください。

@XmlRootElement
@XmlSeeAlso({P1.class, P2.class})
public interface Person {
  @XmlAttribute
  public String getId();
  @XmlElement
  public String getName();
}
于 2013-07-31T01:10:11.987 に答える