2017-10-16 193 views
0

我想解組一個Java對象的XML響應,儘管解組通過罰款,但Java對象的屬性設置爲null。jaxb unmarshalling給null屬性

這是響應:

<ams:fault> 
    <ams:code>900905</ams:code> 
    <ams:message>Incorrect Access Token Type is provided</ams:message> 
    <ams:description>Access failure for API: /stockquote, version: 1.0.0 with 
key: lI2XVmmRJ9_B_rbh1rwV7Pg3Pp8a</ams:description> 
</ams:fault> 

下面是一個Java類,我凸輪嘗試捕捉和解組內容:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement(name = "fault", namespace = "http://wso2.org/apimanager/security") 
public class SMSAuthFault 
{ 
    @XmlElement(name = "code") 
    private String code; 

    @XmlElement(name = "message") 
    private String message; 

    @XmlElement(name = "description") 
    private String description; 

    public String getCode() 
    { 
     return code; 
    } 

    public void setCode(String code) 
    { 
     this.code = code; 
    } 

    public String getMessage() 
    { 
     return message; 
    } 

    public void setMessage(String message) 
    { 
     this.message = message; 
    } 

    public String getDescription() 
    { 
     return description; 
    } 

    public void setDescription(String description) 
    { 
     this.description = description; 
    } 

    @Override 
    public String toString() 
    { 
     return "SMSAuthFault [code=" + code + ", message=" + message 
       + ", description=" + description + "]"; 
    } 

} 

這裏是我如何試圖來解讀這樣的響應:

JAXBContext jaxb = JAXBContext.newInstance(SMSAuthFault.class); 
Unmarshaller unmarshaller = jaxb.createUnmarshaller(); 
System.out.println(unmarshaller.unmarshal(new StringReader(m))); 

爲上述嘗試打印的響應:

SMSAuthFault [code=null, message=null, description=null] 
+0

凡在你的XML'命名空間定義爲'的xmlns ams'前綴:AMS =「http://wso2.org/apimanager /安全「'?它必須要麼高於fault元素,要麼至少在你的響應第一行中,比如'' – Vadim

回答

0

從Javadoc中的Unmarshaller https://docs.oracle.com/javase/7/docs/api/javax/xml/bind/Unmarshaller.html

從一個StringBuffer使用javax.xml.transform.stream.StreamSource中解組:

JAXBContext jc = JAXBContext.newInstance("com.acme.foo"); 
    Unmarshaller u = jc.createUnmarshaller(); 
    StringBuffer xmlStr = new StringBuffer("<?xml version="1.0"?>..."); 
    Object o = u.unmarshal(new StreamSource(new StringReader(xmlStr.toString()))); 

嘗試使用StreamSource的對象來包裝StringReader

+0

StreamSource構造函數需要uri格式參數,不知道它是如何解決我的問題? –

+0

StreamSource構造函數也需要Reader參數 – Judger

+0

如果不起作用。嘗試將註釋放在setter方法上,因爲這些字段是私人的。 – Judger

0

試試這個代碼:

package org.softdevelop.unmarshall; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 

/** 
* Created by Jorge on 16/10/2017. 
*/ 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement(name = "fault", namespace = "http://wso2.org/apimanager/security") 
public class SMSAuthFault 
{ 
    @XmlElement(name = "code") 
    private String code; 

    @XmlElement(name = "message") 
    private String message; 

    @XmlElement(name = "description") 
    private String description; 

    public String getCode() 
    { 
     return code; 
    } 

    public void setCode(String code) 
    { 
     this.code = code; 
    } 

    public String getMessage() 
    { 
     return message; 
    } 

    public void setMessage(String message) 
    { 
     this.message = message; 
    } 

    public String getDescription() 
    { 
     return description; 
    } 

    public void setDescription(String description) 
    { 
     this.description = description; 
    } 

    @Override 
    public String toString() 
    { 
     return "SMSAuthFault [code=" + code + ", message=" + message 
       + ", description=" + description + "]"; 
    } 

} 

我的主類:

package org.softdevelop.unmarshall; 

import org.w3c.dom.Document; 
import org.w3c.dom.Node; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBElement; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Unmarshaller; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 
import java.io.IOException; 
import java.io.StringReader; 

/** 
* Created by Jorge on 16/10/2017. 
*/ 
public class Unmarshall { 

    public static void main(String [] arg) throws JAXBException, ParserConfigurationException, IOException, SAXException { 
     JAXBContext jaxb = JAXBContext.newInstance(SMSAuthFault.class); 
     Unmarshaller um = jaxb.createUnmarshaller(); 
     String m = "<ams:fault xmlns:ams=\"http://some.uri.net/\">\n" + 
       " <ams:code>900905</ams:code>\n" + 
       " <ams:message>Incorrect Access Token Type is provided</ams:message>\n" + 
       " <ams:description>Access failure for API: /stockquote, version: 1.0.0 with \n" + 
       " key: lI2XVmmRJ9_B_rbh1rwV7Pg3Pp8a</ams:description>\n" + 
       "</ams:fault>"; 

     StringReader sr = new StringReader(m); 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     dbf.setNamespaceAware(true); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     Document doc = db.parse(new InputSource(sr)); 
     Node n = (Node) doc.getDocumentElement(); 
     JAXBElement<SMSAuthFault> personElement = um.unmarshal(n, SMSAuthFault.class); 
     SMSAuthFault smsF = personElement.getValue(); 
     System.out.println(smsF); 
    } 
} 

我的輸出:

SMSAuthFault [code=900905, message=Incorrect Access Token Type is provided, description=Access failure for API: /stockquote, version: 1.0.0 with 
key: lI2XVmmRJ9_B_rbh1rwV7Pg3Pp8a] 
+0

上面的代碼是否需要在SMSAuthFault類中進行一些更改?因爲我仍然得到SMSAuthFault [code = null,message = null,description = null] –

+0

不,我只是在xml字符串中添加一個名稱空間:xmlns:ams =「http://some.uri.net/」 –

+0

ohh男孩....運行確切的代碼,但仍然得到SMSAuthFault [code = null,message = null,description = null] –