[英]Java Object to xml schema custom mapping - Web Service
我在JAX-WS中有一個Web服務,在pom.xml中的maven目標('ws-jwsc')生成了WSDL文件以及輸入和輸出XSD。
我想以不同的方式將Java類的屬性映射到WSDL / XSD模式,如下所示:
我有兩個班級1)客戶2)位置
1.客戶-客戶特定信息
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
public Customer() {
super();
}
public Customer(CustomerType customerType) {
this.customerType = customerType;
}
public enum CustomerType {
B, S, C
}
private CustomerType customerType;
private String name;
private Long accountNumber;
private Location location;
// getter/setter for properties
}
2.位置-包含addr1 / addr2 / city / state / zip / country的位置對象
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Location {
private String address1;
private String address2;
private String city;
private String state;
private String zip;
private String country;
/**
* @return the city
*/`enter code here`
public String getCity() {
return city;
}
//getter/setter for properties
}
現在我的問題是在運行時,Customer類中的“ customerType”屬性可以有3個值('B','C','S')
所以,
例如,如果customerType的運行時值為“ S”。
然后代碼'ShipperAddress1', 'ShipperAddress2', 'ShipperCity', 'ShipperState', 'ShipperZip', 'ShipperCountry' for 'address1' , 'address2', 'city', 'state', 'zip' and 'country'
SOAP響應XML中分別具有'ShipperAddress1', 'ShipperAddress2', 'ShipperCity', 'ShipperState', 'ShipperZip', 'ShipperCountry' for 'address1' , 'address2', 'city', 'state', 'zip' and 'country'
屬性。
示例2:如果customerType的運行時值為“ C”。
那么代碼'ConsigneeAddress1', 'ConsigneeAddress2', 'ConsigneeCity', 'ConsigneeState', 'ConsigneeZip', 'ConsigneeCountry' for 'address1' , 'address2', 'city', 'state', 'zip' and 'country'
SOAP響應XML中分別具有'ConsigneeAddress1', 'ConsigneeAddress2', 'ConsigneeCity', 'ConsigneeState', 'ConsigneeZip', 'ConsigneeCountry' for 'address1' , 'address2', 'city', 'state', 'zip' and 'country'
屬性。
我需要知道是否可以這樣做,如果可以,怎么辦? 非常感謝所有幫助。
您可以很好地做到這一點,使用@XmlElementRef
,然后使用“位置繼承”。
為Location
或AbstractLocation
創建一個基類,然后為Customer Type托運人或收貨人“創建” Location的實例,並覆蓋子類中的元素名稱。 我在下面有一個示例來說明我在哪里顯示一個字段Address1
。 您可以類似地覆蓋所有必填字段。
請注意,我沒有設置Field
訪問類型,而是將其設置為Property
以便我們僅覆蓋方法,並且字段在基類中保持"private"
。 如果您認為可以protected.
字段,也可以進行調整protected.
。
例
在示例中,我只使用了LocationC和LocationS 。 您可以添加LocationB
。
具有主類的Customer.java。 注意@XmlElementRef(name =“ Location”)
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
public CustomerType getCustomerType() {
return customerType;
}
public void setCustomerType(CustomerType customerType) {
this.customerType = customerType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(Long accountNumber) {
this.accountNumber = accountNumber;
}
public GeneralLocation getLocation() {
return location;
}
public void setLocation(GeneralLocation location) {
this.location = location;
}
public Customer() {
super();
}
public Customer(CustomerType customerType) {
this.customerType = customerType;
}
public enum CustomerType {
B, S, C
}
private CustomerType customerType;
private String name;
private Long accountNumber;
@XmlElementRef(name = "Location")
private GeneralLocation location;
public static void main(String[] args) throws Exception {
Customer c = new Customer();
c.setAccountNumber(1111111l);
c.setCustomerType(CustomerType.C);
LocationC loc = new LocationC();
loc.setAddress1("I am address 1");
c.setLocation(loc);
Customer c2 = new Customer();
c2.setAccountNumber(222222l);
c.setCustomerType(CustomerType.S);
LocationS locs = new LocationS();
locs.setAddress1("I am S address 1");
c2.setLocation(locs);
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Marshaller marrshaller = jc.createMarshaller();
marrshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marrshaller.marshal(c, System.out);
marrshaller.marshal(c2, System.out);
}
}
Base AbstractLocation.java(我已經使用注釋@XmlTransient隱藏了它的屬性。
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlTransient;
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlSeeAlso({LocationC.class, LocationS.class})
abstract class GeneralLocation {
private String address1;
private String address2;
private String city;
private String state;
private String zip;
private String country;
@XmlTransient()
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
LocationC.java
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement()
public class LocationC extends GeneralLocation {
@XmlElement(name="ConsigneeAddress1")
@Override
public String getAddress1() {
return super.getAddress1();
}
}
LocationS.java
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement()
@XmlAccessorType(XmlAccessType.PROPERTY)
public class LocationS extends GeneralLocation {
@XmlElement(name="ShipperAddress1")
public String getAddress1() {
return super.getAddress1();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.