【问题标题】:jaxb unmarshalling giving null attributesjaxb 解组给空属性
【发布时间】:2017-10-16 16:48:00
【问题描述】:

我正在尝试解组对 Java 对象的 XML 响应,尽管解组过程正常,但 Java 对象的属性设置为 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]

【问题讨论】:

  • 在您的 xml ams 前缀中为命名空间定义为 xmlns:ams="http://wso2.org/apimanager/security" 的位置?它必须在高于故障元素或至少在您的响应第一行中,例如&lt;ams:fault xmlns:ams="http://wso2.org/apimanager/security"&gt;

标签: java xml jaxb wso2 jax-rs


【解决方案1】:

来自 Unmarshaller 的 javadoc https://docs.oracle.com/javase/7/docs/api/javax/xml/bind/Unmarshaller.html

使用 javax.xml.transform.stream.StreamSource 从 StringBuffer 解组:

   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

【讨论】:

  • StreamSource 构造函数需要 uri 格式参数,不知道它如何解决我的问题?
  • StreamSource 构造函数也接受 Reader 参数
  • 如果它不起作用。尝试将注释放在 setter 方法上,因为这些字段是私有的。
【解决方案2】:

试试这个代码:

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]

【讨论】:

  • 上述代码是否也需要对 SMSAuthFault 类进行一些更改?因为我仍然收到 SMSAuthFault [code=null, message=null, description=null]
  • 天哪.... 运行准确的代码但仍然收到 SMSAuthFault [code=null, message=null, description=null]
  • java 版本 "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
  • java 版本“1.8.0_112”Java(TM) SE 运行时环境(内部版本 1.8.0_112-b15)Java HotSpot(TM) 客户端 VM(内部版本 25.112-b15,混合模式)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-04
  • 2014-09-18
  • 1970-01-01
  • 2018-02-14
  • 2010-12-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多