2014-10-20 88 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