JAXB 不同子类使用相同的元素名称

要求云主机、带宽、云硬盘配置的元素名称都为itemConfig,并且能根据对应的类生成正确的元素 。

使用@XmlMixed 和 @XmlElementRefs 可实现

//订单选项配置
@XmlTransient
public class OrderItemConfig {
	
}

 

//订单云主机配置
@XmlRootElement(name = "itemConfig")
public class VMCItemConfig extends OrderItemConfig{
	private String memSize;
	private String cpuNum;
	private String name;	
}
//订单带宽配置
@XmlRootElement(name = "itemConfig")
public class NETWORKItemConfig extends OrderItemConfig{
	private String type;
	private String name;
	private String regionId;
}
//订单云硬盘配置
@XmlRootElement(name = "itemConfig")
public class EBSCItemConfig extends OrderItemConfig {
	private String volumeType;
}
public class OrderItem {
	private String master;
	private OrderItemConfig itemConfig;

	@XmlMixed
    @XmlElementRefs({
            @XmlElementRef(type=NETWORKItemConfig.class),
            @XmlElementRef(type=VMCItemConfig.class),
            @XmlElementRef(type=EBSCItemConfig.class)})
	public void setItemConfig(OrderItemConfig itemConfig) {
		this.itemConfig = itemConfig;
	}

}
            
                
                    1
                    4
                    pc-1
                
                VMC

            
            
                
                    SATA
                
                EBSC

            
            
                
                    bandwith-001
                    gz-t
                    standalone
                
                NETWORK
            

参考:https://stackoverflow.com/questions/10940267/jaxb-xmlmixed-usage-for-reading-xmlvalue-and-xmlelement/11099303#11099303

你可能感兴趣的:(笔记)