2011-04-22 46 views
3

我目前正在學習JAXB和Web服務,並且我有這個問題,我不知道 如何解決。在JAXB中創建SOAP屬性

假設我有這個非常簡單的類,我註釋了JAXB。

@XmlRootElement 
public class Customer { 
    private int custID; 
    private String custName; 
    //getters and setters 
} 

我有這個類,我暴露作爲Web服務。 (注:我在這裏一切都硬編碼爲簡單起見 但這連接到DB)

@WebService 
public class CustomerWS { 

    @WebMethod(operationName = "customerData") 
    public Customer getCustomer(){ 
     Customer cust = new Customer(); 
     cust.setCustID(12345); 
     cust.setCustName("John Doe");  
     return cust; 
    } 
} 

SOAP信封的反應是這樣的。

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
    <S:Body> 
     <ns2:customerDataResponse xmlns:ns2="http://test.com/"> 
     <return> 
      <custID>12345</custID> 
      <custName>John Doe</custName> 
     </return> 
     </ns2:customerDataResponse> 
    </S:Body> 
</S:Envelope> 

現在假設我在客戶的實體,稱爲地位的另一個屬性,他們希望這個屬性爲屬性肥皂 迴應像下面而不是客戶元素的一部分。 (A =有效,I =無效)

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
    <S:Body> 
     <ns2:customerDataResponse status="A" xmlns:ns2="http://test.com/"> 
     <return> 
      <custID>12345</custID> 
      <custName>John Doe</custName> 
     </return> 
     </ns2:customerDataResponse> 
    </S:Body> 
</S:Envelope> 

@XmlRootElement 
public class Customer { 
    private int custID; 
    private String custName; 

    //Another Annotation??? (A or I only) 
    private String status; 
    //getters and setters 
} 

如何註釋我的班級以滿足此要求?謝謝

回答

4

客戶類中標註的所有內容都將相對於客戶元素。

這是因爲JAX-WS負責形成消息信封,然後JAXB將消息的正文編組到這個信封中。當JAXB組織身體的時候,改變信封已經太遲了。

+0

感謝您的回覆。有沒有其他選擇或解決方法,以便我可以做我想做的事情?另外,是否有一種方法可以將任何屬性硬編碼到信封中,即使它不是任何實體的一部分(只是硬編碼任何實體..)? – 2011-04-22 11:48:18