将java对象转换为java xml对象

时间:2014-10-20 20:36:08

标签: java xml spring web-services jaxb

我正构建一个简单的Spring Web应用程序,该应用程序在此对象中获取用户地址信息和存储 - Contactinfo。此Contactinfo将传递给Web服务,该服务将行插入数据库..

CONTACTINFO。

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

因为我正在构建Web应用程序和Web服务,所以我试图在我的Web服务中重用Contactinfo ...所以,我的服务看起来像这样 -

@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对象)传递给我的webservice(需要XML对象)..

我看到博客将Java对象转换为XML,反之亦然,但没有将POJO对象转换为Java XML对象......

1 个答案:

答案 0 :(得分:0)

您可以使用Dozer框架(http://dozer.sourceforge.net/)..

它为你提供了一个api来创建另一个对象(具有相似的结构)

示例:

Mapper mapper = new DozerBeanMapper();

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