java.util.Listはインターフェースであり、JAXBはインターフェースを処理できません
-
08-07-2019 - |
質問
アプリケーションをデプロイしようとすると、次の例外が発生するようです:
Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
java.util.List is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
at java.util.List
at private java.util.List foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse._return
at foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse
java.util.List does not have a no-arg default constructor.
this problem is related to the following location:
at java.util.List
at private java.util.List foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse._return
at foobar.alkohol.register.webservice.jaxws.GetRelationsFromPersonResponse
リストからリストに戻り型を変更するまで、私のコードはうまく機能しました<!> lt; List <!> lt; RelationCanonical <!> gt; <!> gt;
部分的なWebサービスは次のとおりです。
@Name("relationService")
@Stateless
@WebService(name = "RelationService", serviceName = "RelationService")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public class RelationService implements RelationServiceLocal {
private boolean login(String username, String password) {
Identity.instance().setUsername(username);
Identity.instance().setPassword(password);
Identity.instance().login();
return Identity.instance().isLoggedIn();
}
private boolean logout() {
Identity.instance().logout();
return !Identity.instance().isLoggedIn();
}
@WebMethod
public List<List<RelationCanonical>> getRelationsFromPerson(@WebParam(name = "username")
String username, @WebParam(name = "password")
String password, @WebParam(name = "foedselsnummer")
String... foedselsnummer) {
......
......
......
}
@SOAPBindingを削除してデフォルトを試してみましたが、同じ結果になります。
ヘルプに感謝します
更新
何かを書き留めたい。すべてのリストをArrayListに変更し、コンパイルしました。私がコンパイルされて動作しなかったと言う理由は、奇妙に振る舞うからです。タイプのオブジェクトを取得します: RelationServiceStub.ArrayList しかし、オブジェクトにはgetメソッドがないか、リストとしても動作しません。また、リストにキャストしようとしましたが、うまくいきませんでした。
これは、Axis 2とwsdl2javaを使用した後ですので、はい、今はコンパイルされますが、データを取得する方法がわかりません。
解決
私の理解では、JAXBはXMLに変換する方法を知らないため、JAXBを介してプレーンList
を処理することはできません。
代わりに、List<RelationCanonical>
を保持するJAXBタイプ(Type1
と呼ぶ)を定義し、それらのタイプのリストを保持する別のタイプを順番に定義する必要があります(処理中) a List<List<...>>
;このタイプをType2
)と呼びます。
結果は、次のようなXML出力になる可能性があります。
<Type2 ...>
<Type1 ...>
<RelationCanonical ...> ... </RelationCanonical>
<RelationCanonical ...> ... </RelationCanonical>
...
</Type1>
<Type1>
<RelationCanonical ...> ... </RelationCanonical>
<RelationCanonical ...> ... </RelationCanonical>
...
</Type1>
...
</Type2>
2つのJAXB注釈付き型を囲むことなく、JAXBプロセッサはどのマークアップを生成するかわからないため、失敗します。
-編集:
つまり、次のようになります。
@XmlType
public class Type1{
private List<RelationCanonical> relations;
@XmlElement
public List<RelationCanonical> getRelations(){
return this.relations;
}
public void setRelations(List<RelationCanonical> relations){
this.relations = relations;
}
}
and
@XmlRootElement
public class Type2{
private List<Type1> type1s;
@XmlElement
public List<Type1> getType1s(){
return this.type1s;
}
public void setType1s(List<Type1> type1s){
this.type1s= type1s;
}
}
J5EEチュートリアルの JAXBセクションも確認してください。 および非公式JAXBガイド。
他のヒント
それが目的に合っていれば、いつでも次のような配列を定義できます:
YourType[]
JAXBはそれが何であるかを確実に把握でき、クライアント側ですぐに使用できるはずです。また、リストからではなく、Webサービスによって提供されるメソッドを介してサーバーから取得した配列を変更できないため、そのようにすることをお勧めします。
任意のクラスでこれを実行する場合。
return items.size() > 0 ? items.toArray((Object[]) Array.newInstance(
items.get(0).getClass(), 0)) : new Object[0];
<!> quot; ArrayList <!> quot;を使用できます。内側の<!> quot; List <!> quot;
の代わりに