Java code calls to pass objects using CXF built WebService service

Source: Internet
Author: User

Good one CXF's Webservcie service was successfully created in front of it with a parameterless method. Now we have further tried to use the method with parameters, and measured the parameters of string and the custom object respectively.


There is no difference between using string as a parameter and without arguments, which is called a trivial invocation. However, when invoking the method of the custom object as a parameter, the actual data is not successful, so after searching on the internet, we find a solution to achieve this function successfully.


The specific implementation steps are as follows:

1. Add a custom class to the service project:

Package Models;public class Usermodel {public    Usermodel () {        super ();    }    Public Usermodel (String name, Int. age) {        super ();        this.name = name;        This.age = age;    }    /**     * Name * * *    private String name;    /**     * Age */    private int;    @Override public    String toString () {        return ' Usermodel [name= "+ name +", age= "+ Age +"] ";    }}

2. Add a method to the service:
Public Usermodel AddUser (@WebParam (name = "user") Usermodel user);

3, the implementation of the method:
/**     * Custom object is parameter     *      * @author: Tuzongxun     * @Title: AddUser     * @Description: TODO     * @param @param User     * @param @return     * @date June, 9:28:38 AM     * @throws */    @Override public    Usermodel A Dduser (Usermodel user) {        System.out.println (user);        return user;    }

4, in the project of the simulation client also add the same package name path of the same custom Usermodel class (because previously on the Xfire, because the package name path of the different problems have been pits, so here to write directly, then do not test is also must be the same), Add the same method as the service in the related services (the code is skipped);
5, called in the Main method of the simulation client, and the same way as before, just change the method of the call:
Package Cxftest1;import models. Usermodel;import Org.apache.cxf.jaxws.jaxwsproxyfactorybean;public class Cxftest {public    static void main (String [] args) {        Jaxwsproxyfactorybean Factorybean = new Jaxwsproxyfactorybean ();        Factorybean.getininterceptors (). Add (New Loggingininterceptor ());        Factorybean.getoutinterceptors (). Add (New Loggingoutinterceptor ());        Factorybean.setserviceclass (cxftestservice.class);        Factorybean                . Setaddress ("Http://localhost:8082/cxfTest/services/CxfTestService");        Cxftestservice Impl = (cxftestservice) factorybean.create ();        System.out.println (Impl.getusername ());        System.out.println (Impl.addusername ("Hello"));        System.out.println (Impl.adduser (New Usermodel ("UName"));}    }

6, the results printed out, the data transmission is actually a failure:


7, after finding the information on the Internet to find a solution, after adding some annotations on the custom class, successfully passed and returned the data of the custom object, the modified custom object is as follows:
Package Models;import Javax.xml.bind.annotation.xmlaccesstype;import Javax.xml.bind.annotation.XmlAccessorType; Import Javax.xml.bind.annotation.xmlrootelement;import Javax.xml.bind.annotation.XmlType; @XmlRootElement (name = " Usermodel ") @XmlAccessorType (Xmlaccesstype.field) @XmlType (Proporder = {" Name "," Age "}) public class Usermodel {    Public Usermodel () {        super ();    }    Public Usermodel (String name, Int. age) {        super ();        this.name = name;        This.age = age;    }    /**     * Name * * *    private String name;    /**     * Age */    private int;    @Override public    String toString () {        return ' Usermodel [name= "+ name +", age= "+ Age +"] ";    }}

8. Once again called in Main, the console prints

9, the above annotation related explanation is as follows:

@XmlRootElement-Specify the XML root element name (optional)

@XmlAccessorType-control property or method serialization

Four options:

Field-is automatically bound to XML for each non-static, non-transient attribute JAXB tool, unless noted xmltransient

None-does not do any processing

Property-binds a property with a Set/get method unless it is noted xmltransient

Public_member-Binds a property with a Set/get method or a property with public access, unless the note

Ming Xmltransient

@XmlType-Maps A class or an enumeration type into an XML Schema type


10. Reference: http://hbiao68.iteye.com/blog/2044252


Java code calls to pass objects using CXF built WebService service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.