java - JAXB UnMarshalling 呈现空值(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 类。我得到了一个 JAXB 类(为此使用了反编译器):
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);
}
}
现在为了把它弄出来,我写了一个简单的类,它只接受一个字符串并解组它:
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());
}
}
但是最后一行打印为空。我期待它打印 value 16365343278450M
。事实上,aMessage
对象中的每个值都是空的(范围、类型等)。
我怀疑我写的 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>";
或更改 XSDelementFormDefault
中的 XSD 并保留此原始字符串而无需命名空间。
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.gmt.com/provisioning/gtc/xml/Messaging" elementFormDefault="unqualified" attributeFormDefault="unqualified">
我选择了后者,因为它对我来说更易于管理。