2014-10-20 148 views
0

我正在構建一個簡單的Spring Web應用程序,它獲取用戶地址信息並存儲在此對象中 - Contactinfo。這Contactinfo將被傳遞到一個Web服務,這將插入行到數據庫..將Java對象轉換爲java xml對象

Contactinfo。

public class ContactInfo { 
    String addr1; 
    String addr2; 
    String city; 
    String state; 
    String pin; 
    String country; 
    String phone; 
    String mobile; 
    String email; 
} 

,因爲我正在創建的Web應用程序和web服務,我想重用CONTACTINFO在我的web服務......所以,我的服務是這樣的 -

@WebService 
@SOAPBinding(style = SOAPBinding.Style.RPC) 
public class HelloWebService{ 
    @WebMethod(operationName = "sayHello") 
    public String sayHello(@WebParam(name="guestname") Contactinfo contactinfo){ 
     return "Hello "; 

    } 
} 

和生成的WSDL CONTACTINFO類看起來是這樣的 -

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "contactInfo", propOrder = { 
    "addr1", 
    "addr2", 
    "city", 
    "country", 
    "email", 
    "mobile", 
    "phone", 
    "pin", 
    "state" 
}) 
public class ContactInfo { 

    protected String addr1; 
    protected String addr2; 
    protected String city; 
    protected String country; 
    protected String email; 
    protected String mobile; 
    protected String phone; 
    protected String pin; 
    protected String state; 

    /** 
    * Gets the value of the addr1 property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getAddr1() { 
     return addr1; 
    } 

    /** 
    * Sets the value of the addr1 property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setAddr1(String value) { 
     this.addr1 = value; 
    } 

    /** 
    * Gets the value of the addr2 property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getAddr2() { 
     return addr2; 
    } 

    /** 
    * Sets the value of the addr2 property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setAddr2(String value) { 
     this.addr2 = value; 
    } 

    /** 
    * Gets the value of the city property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getCity() { 
     return city; 
    } 

    /** 
    * Sets the value of the city property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setCity(String value) { 
     this.city = value; 
    } 

    /** 
    * Gets the value of the country property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getCountry() { 
     return country; 
    } 

    /** 
    * Sets the value of the country property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setCountry(String value) { 
     this.country = value; 
    } 

    /** 
    * Gets the value of the email property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getEmail() { 
     return email; 
    } 

    /** 
    * Sets the value of the email property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setEmail(String value) { 
     this.email = value; 
    } 

    /** 
    * Gets the value of the mobile property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getMobile() { 
     return mobile; 
    } 

    /** 
    * Sets the value of the mobile property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setMobile(String value) { 
     this.mobile = value; 
    } 

    /** 
    * Gets the value of the phone property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getPhone() { 
     return phone; 
    } 

    /** 
    * Sets the value of the phone property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setPhone(String value) { 
     this.phone = value; 
    } 

    /** 
    * Gets the value of the pin property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getPin() { 
     return pin; 
    } 

    /** 
    * Sets the value of the pin property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setPin(String value) { 
     this.pin = value; 
    } 

    /** 
    * Gets the value of the state property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getState() { 
     return state; 
    } 

    /** 
    * Sets the value of the state property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setState(String value) { 
     this.state = value; 
    } 

} 

現在,我需要知道我怎麼可以傳遞的ContactInfo(普通POJO對象)到我的web服務(這需要一個XML對象)..

我看到博客的Java對象轉換爲XML,反之亦然,但沒有轉換一個POJO對象到Java XML對象...

+0

我不知道你的問題。你想知道如何調用這個Web服務(可能來自遠程系統)? – 2014-10-20 20:44:03

+0

不,我可以調用webservice ..我需要將Java POJO Object轉換爲Java XML Object而不轉換爲XML文件。 – user1050619 2014-10-20 20:46:51

+0

什麼是Java XML對象?你的意思是你的ContactInfo對象的字符串表示? – mendieta 2014-10-20 20:53:27

回答

0

您可以使用推土機架構(http://dozer.sourceforge.net/)..

它給你的API來創建另一個從一個對象(具有類似的結構)

例子:

Mapper mapper = new DozerBeanMapper(); 

DestinationObject destObject = new DestinationObject(); 
mapper.map(sourceObject, destObject); 
+1

謝謝..這就是我要找的東西..找到一篇文章以及任何人蔘考 - http://www.javaworld.com/article/2074949/core-java/dozer--mapping-jaxb-objects-to-business-domain-objects.html – user1050619 2014-10-20 21:09:44