67,547
社区成员
发帖
与我相关
我的任务
分享
@XmlRootElement(name = "RSP")
@XmlSeeAlso({ QueryOut.class, SubmissionOut.class })
public class Response<T> {
private ResponseHead respHead;
@XmlAnyElement(lax = true)
private T t;
@XmlElement(name = "PUB")
public ResponseHead getRespHead() {
return respHead;
}
public void setRespHead(ResponseHead respHead) {
this.respHead = respHead;
}
@XmlTransient
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
package platform.interfaces.entity
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "OUT")
public class QueryOut {
/** 指令批次号 */
private String reqSeqID;
/** 批次处理状态 */
private String reqSeqState;
private List<QueryRd> rd;
@XmlElement(name = "ReqSeqID")
public String getReqSeqID() {
return reqSeqID;
}
public void setReqSeqID(String reqSeqID) {
this.reqSeqID = reqSeqID;
}
@XmlElement(name = "ReqSeqState")
public String getReqSeqState() {
return reqSeqState;
}
public void setReqSeqState(String reqSeqState) {
this.reqSeqState = reqSeqState;
}
@XmlElement(name = "RD")
public List<QueryRd> getRd() {
return rd;
}
public void setRd(List<QueryRd> rd) {
this.rd = rd;
}
}
package platform.interfaces.entity
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* 数据提交<br>
* 批量代付代扣指令提交应答 <br/>
* <h1>指令特定元素</h1>
*
* @author admin
*
*/
@XmlRootElement(name = "OUT")
public class SubmissionOut {
/** 指令批次号 */
private String reqSeqID;
/** 保留字段 */
private String reqReserved1;
/** 保留字段 */
private String reqReserved2;
@XmlElement(name = "ReqSeqID")
public String getReqSeqID() {
return reqSeqID;
}
public void setReqSeqID(String reqSeqID) {
this.reqSeqID = reqSeqID;
}
@XmlElement(name = "ReqReserved1")
public String getReqReserved1() {
return reqReserved1;
}
public void setReqReserved1(String reqReserved1) {
this.reqReserved1 = reqReserved1;
}
@XmlElement(name = "ReqReserved2")
public String getReqReserved2() {
return reqReserved2;
}
public void setReqReserved2(String reqReserved2) {
this.reqReserved2 = reqReserved2;
}
}
public static void main(String[] args) throws JAXBException {
System.out.println("==========================");
String result = "<?xml version=\"1.0\" encoding=\"utf-8\"?><RSP><PUB><TransSource>LIS</TransSource><TransCode>1998</TransCode><TransDate>20130730</TransDate><TransTime>125955</TransTime><TransSeq>201307301259550117</TransSeq></PUB><OUT><RD><ReqSeqID>20130115030000AP002178</ReqSeqID><RdSeq>20130115030000AP002178-1</RdSeq><RtnCode>success</RtnCode><RtnMsg>通知成功</RtnMsg></RD></OUT></RSP>";
JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Response<QueryOut> req = (Response<QueryOut>) unmarshaller.unmarshal(new StringReader(result));
System.out.println(req);
}
<Transaction>
<Transaction_Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="testReqBody">
....
</Transaction_Body>
</Transaction>
这个 xsi:type 属性里面的内容是不是很熟悉,没错就是我们转换时的泛型首字母小写,该类在@XmlSeeAlso注解中也有定义
@XmlSeeAlso({TestReqBody.class, TestRespBody.class})
public class Transaction<T> {
@XmlElement(name = "Transaction_Body")
private T transactionBody;
...
}
这样就能解释为什么我们自己的 xml 转换总是以注解@XmlSeeAlso最后一个类为准,因为转换时根据 xml 找不到具体类型,就以最后一个为准,那么我们自己的 xml 在向泛型类转换时,可以加上以上属性即可转换成功,这个时候问题来了,以上属性会变化吗?属性值的来源是哪里?
通过测试,发现与以下注解有关(具体有什么样的关联,各位自己测试,这里就不举例了)@XmlType(name = "xxx", namespace = "xxx")
这样我们就可以在转换的时候,根据自己 @XmlType 上定义的属性,通过正则表达式将泛型标签对应的属性和名称空间添加上即可。 <!-- apache cxf -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.2.5</version>
</dependency>
<!-- apache cxf end -->