2012-03-03 8 views
2

RING APIを使用してBing Translate用のJavaクライアントを作成しています。私はOAuthで認証して問題のない翻訳を実行でき、問題のない単純なStringレスポンスをJAXBオブジェクトにアンマーシャリングすることができます。BingからのREST応答のアンマーシャリング

しかし、もっと複雑な型になると、私がJavaオブジェクトのフィールドで常にnull値を取得している理由を理解しようとしています。

<ArrayOfstring 
    xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" 
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
    <string>ar</string> 
    <string>bg</string> 
    <string>ca</string> 
    <string>zh-CHS</string> 
    <string>zh-CHT</string> 
</ArrayOfstring> 

私は次のメソッド使用してオブジェクトを非整列化しています:私はサービスから取得応答がある

@SuppressWarnings("unchecked") 
    public static <T extends Object> T unmarshallObject(Class<T> clazz, InputStream stream) 
    { 
    T returnType = null; 

    try 
    { 
     JAXBContext jc = JAXBContext.newInstance(clazz); 
     Unmarshaller u = jc.createUnmarshaller(); 

     returnType = (T) u.unmarshal(stream); 
    } catch (Exception e1) 
    { 
     e1.printStackTrace(); 
    } 

    return returnType; 

    } 

単純なオブジェクトのために正常に動作しますので、私はこの問題は私の注釈内にある疑いがあります私が生成しようとしている複雑なオブジェクトです。そのためのコードは次のとおりです。必死で

package some.package; 
import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 

import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAnyElement; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlElementWrapper; 
import javax.xml.bind.annotation.XmlElements; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.XmlValue; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement(name="ArrayOfstring", namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays") 
public class ArrayOfString implements Serializable 
{ 

    @XmlElement(name="string", namespace="http://schemas.microsoft.com/2003/10/Serialization") 
    private List<String> string; 

    public List<String> getString() 
    { 
    return string; 
    } 

    public void setString(List<String> strings) 
    { 
    this.string = strings; 
    } 

} 

、私は@XmlAnyElementで@XmlElement(名前=「文字列」)を交換していないと私は戻って文字列のリストを得たが、何の値。

私の質問は、上記のXMLを正しく解釈するために何を変更する必要があるのか​​、さらに重要なのはなぜですか?

答えて

1

例では、string要素は実際にはhttp://schemas.microsoft.com/2003/10/Serialization/Arrays名前空間に属しています。

あなたの注釈には、http://schemas.microsoft.com/2003/10/Serialization名前空間が必要です。

ではなく

@XmlElement(name="string", 
    namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays") 
private List<String> string; 

を試してみてください。

+0

それはすごくうまくいった。私は、 'string'が_http://schemas.microsoft.com/2003/10/Serialization_名前空間の下にあるオブジェクトであると仮定して作業していましたが、配列内の要素に使用するのは同じ名前空間でなければなりません。私は間違っていた。ありがとう! – Benemon