例如,下面这段XML数据,有多个data元素,并且它们级别相同:
<?xml version="1.0" encoding="UTF-8"?><request><reqtype>05</reqtype><secret>test</secret><body><userid>15</userid><seeid>1001</seeid><time>202311201510</time><data><type>01</type><value>219</value></data><data><type>02</type><value>217</value></data></body>
</request>
要用JAXB注解将Java类映射到上面的XML,示例如下:
映射xml request元素、及下面一级子元素的Java类:
package com.thb.server.fulldata;import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlType;/*** 该类映射到http请求的xml* @author thb**/
// 使用了JAXB注解,映射到xml中的request元素
@XmlRootElement(name = "request")
@XmlType(propOrder = {"reqtype", "secret", "fullDataContent"})
public class FullDataRequest {private String reqtype;private String secret;private FullDataContent fullDataContent;// 使用了JAXB注解,映射到xml中的reqtype元素@XmlElement(name="reqtype", required = true)public String getReqtype() {return this.reqtype;}// 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值public void setReqtype(String reqtype) {this.reqtype = reqtype;}// 使用了JAXB注解,映射到xml中的secret元素@XmlElement(name="secret", required = true)public String getSecret() {return this.secret;}// 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值public void setSecret(String secret) {this.secret = secret;}// 使用了JAXB注解,映射到xml中的body元素@XmlElement(name="body", required = true)public FullDataContent getFullDataContent() {return this.fullDataContent;}public void setFullDataContent(FullDataContent fullDataContent) {this.fullDataContent = fullDataContent;}
}
映射xml body子元素、及下面一级子元素的Java类:
package com.thb.server.fulldata;import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlType;@XmlType(propOrder = {"userid", "seeid", "time", "items"})
class FullDataContent {private String userid;private String seeid;private String time;private Item[] items;// 使用了JAXB注解,映射到xml中body元素下面的userid元素@XmlElement(name="userid", required = true)public String getUserid() {return this.userid;}// 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值public void setUserid(String userid) {this.userid = userid;}// 使用了JAXB注解,映射到xml中body元素下面的seeid元素@XmlElement(name="seeid", required = true)public String getSeeid() {return this.seeid;}// 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值public void setSeeid(String seeid) {this.seeid = seeid;}// 使用了JAXB注解,映射到xml中body元素下面的time元素@XmlElement(name="time", required = true)public String getTime() {return this.time;}// 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值public void setTime(String time) {this.time = time;}// 使用了JAXB注解,映射到xml中body元素下面的data元素@XmlElement(name="data", required = true)public Item[] getItems() {return this.items;}// 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值public void setItems(Item[] items) {this.items = items;}
}
映射到xml data子元素、及下面一级子元素的Java类:
package com.thb.server.fulldata;import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlType;@XmlType(propOrder = {"type", "value"})
public class Item {private String type;private String value;// 使用了JAXB注解,映射到xml中body元素下面-》data子元素下面-》type子元素@XmlElement(name="type", required = true)public String getType() {return this.type;}// 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值public void setType(String type) {this.type = type;}// 使用了JAXB注解,映射到xml中body元素下面-》data子元素下面-》value子元素@XmlElement(name="value", required = true)public String getValue() {return this.value;}// 此处的setter函数要有,否则从xml反序列到java对象的时候无法赋值public void setValue(String value) {this.value = value;}
}
下面来生成Java类的xml schema文件,运行
schemagen -d D:\temp\outschema -cp D:\temp\eclipse-workspace\java_work\power-restful-webservice-server\src\main\java D:\temp\eclipse-workspace\java_work\power-restful-webservice-server\src\main\java\com\thb\server\fulldata\FullDataRequest.java
生成的schema文件内容:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="request" type="fullDataRequest"/><xs:complexType name="fullDataRequest"><xs:sequence><xs:element name="reqtype" type="xs:string"/><xs:element name="secret" type="xs:string"/><xs:element name="body" type="fullDataContent"/></xs:sequence></xs:complexType><xs:complexType name="fullDataContent"><xs:sequence><xs:element name="userid" type="xs:string"/><xs:element name="seeid" type="xs:string"/><xs:element name="time" type="xs:string"/><xs:element name="data" type="item" maxOccurs="unbounded"/></xs:sequence></xs:complexType><xs:complexType name="item"><xs:sequence><xs:element name="type" type="xs:string"/><xs:element name="value" type="xs:string"/></xs:sequence></xs:complexType>
</xs:schema>
将web服务部署到Tomcat,用Postman访问,成功返回了响应:
在服务端正确打印了映射到Java对象中的信息: