简体   繁体   中英

Converting java object into java xml object

I'm building a simple Spring web application that gets user address information and stores in this object - Contactinfo. This Contactinfo will be passed to a web service which will insert the rows into a database..

Contactinfo.

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

since I'm building the web application and the webservice, I'm trying to reuse the Contactinfo in my web service...So, my service looks like this -

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

    }
}

and the generated wsdl Contactinfo class looks like this -

@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;
    }

}

Now, I need to know how can I pass the ContactInfo(Plain POJO Object) to my webservice(which needs a XML Object)..

I see blogs to convert Java Object to XML and vice-versa but nothing to convert a POJO Object to Java XML object...

You can use Dozer framework ( http://dozer.sourceforge.net/ )..

It gives you an api to create one object from another (with similar structure)

Example:

Mapper mapper = new DozerBeanMapper();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM