2012-03-03 2 views
2

RING API를 사용하여 Bing Translate 용 Java 클라이언트를 작성 중입니다. OAuth로 인증하고 아무런 문제없이 번역을 실행하면 문제가없는 간단한 String 응답을 JAXB 객체로 비 정렬화할 수 있습니다.Bing에서 REST 응답을 언 마샬링 Java로 번역

그러나 더 복잡한 유형의 경우 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

관련 문제