JAXB UnMarshalling 呈现空值(JAXB,Java 1.8)

JAXB UnMarshalling Renders Null Values (JAXB, Java 1.8)

我有一个 XSD 是这样写的:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.gmt.com/provisioning/gtc/xml/Messaging" elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xs:element name="GTCMessage">
        <xs:annotation>
            <xs:documentation>
                GTCMessage - To Pass Around using JMS.
            </xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element name="type" type="xs:int" minOccurs="0"/>
                <xs:element name="scope" type="xs:int" minOccurs="0"/>
                <xs:element name="code" type="xs:int" minOccurs="0"/>
                <xs:element name="target" type="xs:string" minOccurs="0"/>
                <xs:element name="message" type="xs:string" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    
</xs:schema>

现在我使用 CXF maven 插件生成 JAXB classes。我得到了一个 JAXB class 之类的(为此使用了反编译器):

import com.gmt.provisioning.gtc.xml.messaging.GTCMessage;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.cxf.xjc.runtime.JAXBToStringStyle;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"type", "scope", "code", "target", "message"})
@XmlRootElement(name = "GTCMessage")
public class GTCMessage {
  protected Integer type;
  
  protected Integer scope;
  
  protected Integer code;
  
  protected String target;
  
  protected String message;
  
  public Integer getType() {
    return this.type;
  }
  
  public void setType(Integer value) {
    this.type = value;
  }
  
  public boolean isSetType() {
    return (this.type != null);
  }
  
  public Integer getScope() {
    return this.scope;
  }
  
  public void setScope(Integer value) {
    this.scope = value;
  }
  
  public boolean isSetScope() {
    return (this.scope != null);
  }
  
  public Integer getCode() {
    return this.code;
  }
  
  public void setCode(Integer value) {
    this.code = value;
  }
  
  public boolean isSetCode() {
    return (this.code != null);
  }
  
  public String getTarget() {
    return this.target;
  }
  
  public void setTarget(String value) {
    this.target = value;
  }
  
  public boolean isSetTarget() {
    return (this.target != null);
  }
  
  public String getMessage() {
    return this.message;
  }
  
  public void setMessage(String value) {
    this.message = value;
  }
  
  public boolean isSetMessage() {
    return (this.message != null);
  }
  
  public String toString() {
    return ToStringBuilder.reflectionToString(this, JAXBToStringStyle.MULTI_LINE_STYLE);
  }
}

现在把它弄出来,我写了一个简单的 class,它只接受一个字符串并将其解组:

public class Test {

    public static void main (String args[]) {

        String abc = "<GTCMessage><type>1</type><scope>2</scope><code>1</code><message>16365343278450M</message></GTCMessage>";

        GTCMessage aMessage = JAXB.unmarshal(new StringReader(abc), GTCMessage.class);

        System.out.println(aMessage.getMessage());
    }
}

但是最后一行打印的是空值。我期待它打印值 16365343278450M。事实上,aMessage 对象中的每个值都是 null(范围、类型等)。

我怀疑我写的 XSD 可能有问题,导致它像多米诺骨牌效应一样出错。

任何指示都会有所帮助。提前致谢。

我能够自己修复它。有两种方法。

首先是将字符串更改为:

String abc = "<GTCMessage xmlns=\"http://www.gmt.com/provisioning/gtc/xml/Messaging\"><type>1</type><scope>2</scope><code>1</code><message>16365343278450M</message></GTCMessage>";

或更改 XSD 中的 XSD elementFormDefault 并保留不带命名空间的原始字符串。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.gmt.com/provisioning/gtc/xml/Messaging" elementFormDefault="unqualified" attributeFormDefault="unqualified">

我选择了后者,因为它对我来说更易于管理。