Java program calls Xfire published WebService service (II)

Source: Internet
Author: User

In the last article called Xfire released by the WebService service, I just returned a string from the server to the client, but did not test the delivery of the data from the client to the server. In practical applications, it is generally unlikely that this scenario will occur, so I have further tested the client to pass the data to the server.
Because the relevant jar package has been mentioned in the previous article, it is no longer repeated, this time the test steps are as follows:
first, the test passes the string to the service side(Focus on the second one):
To further understand the service setup, I have re-wrote a service-side service class:
Interface:
Package Xfiretest;public interface Xfiretestservice {public    string Test (String cont);

Implementation class:
Package Xfiretest.xfiretestimp;import Xfiretest.xfiretestservice;public class Xfiretestimp implements Xfiretestservice {    @Override public    string Test (String cont) {        cont = "WebService received message:" + cont;        return cont;}    }

The following code was added to the previous services.xml:
<service xmlns= "http://xfire.codehaus.org/config/1.0" >        <!--WebService Service name---        <name> xfiretest</name>        <namespace>http://xfireTest/XFireTestService</namespace>        <!-- The interface path you have written--        <serviceClass>            xfiretest.xfiretestservice        </serviceClass>        <!-- Implementing Classpath-        <implementationClass>            xfireTest.xfireTestImp.XFireTestImp        </ Implementationclass>        </service>

Then there is another project in which to impersonate the client calling code,It is also necessary to first create the same service interface as the server:
Package Test;public interface Xfiretestservice {public    string Test (String cont);

Then it is called:
Package Test;import Org.codehaus.xfire.xfirefactory;import Org.codehaus.xfire.client.xfireproxyfactory;import Org.codehaus.xfire.service.service;import Org.codehaus.xfire.service.binding.objectservicefactory;public Class CallWebServiceTest1 {public    static void Main (string[] args) {        Service Srmodel = new Objectservicefactory ()                . Create (Xfiretestservice.class);        Xfireproxyfactory factory = new Xfireproxyfactory (xfirefactory                . newinstance (). Getxfire ());//Create factory instance        String Hellourl = "Http://localhost:8082/xfireTest/services/XFireTest";        try {            Xfiretestservice service = (xfiretestservice) factory.create (                    Srmodel, hellourl);            System.out.println (service.test ("Test"));        } catch (Exception e) {            throw new RuntimeException (e);}}    }

The call here is relatively simple, and there is no problem, the string can be passed completely.
second, pass the object when called(emphasis at the end)
This test, I wrote a simple Usermodel class, used as a parameter, according to some information on the Internet, I gave the entity class to serialize, as follows:
Package Xfiretest;import Java.io.serializable;public class Usermodel implements Serializable {    /**     * */    private static final long serialversionuid = -8344776127885486411l;    Public Usermodel () {        super ();    }    Public Usermodel (String userName, int.) {        super ();        This.username = UserName;        This.age = age;    }    /**     * User name */    private String userName;    /**     * User age */    private int;    Public String GetUserName () {        return userName;    }    public void Setusername (String userName) {        this.username = userName;    }    public int getage () {        return age;    }    public void Setage (int.) {        this.age = age;    }    @Override public    String toString () {        return ' Usermodel [username= "+ UserName +", age= "+ Age +"] ";    }}

Then there is the corresponding interface:
Package Xfiretest;import Javax.jws.webparam;import Javax.jws.WebService; @WebService (serviceName = "UserService", targetnamespace = "Http://xfireTest/UserService") public interface UserService {public    Usermodel AddUser (@ Webparam (name = "user") Usermodel user);

Implementation class:
Package Xfiretest.xfiretestimp;import Xfiretest.usermodel;import Xfiretest.userservice;public class UserServiceImp Implements UserService {    @Override public    usermodel addUser (usermodel user) {        //System.out.println (user) ;        System.out.println (User.getusername () + ":" + user.getage ());        return user;    }}

Add the following code to the Services.xml:
<service xmlns= "http://xfire.codehaus.org/config/1.0" >        <!--WebService Service name---        <name> userservice</name>        <namespace>http://xfireTest/UserService</namespace>        <!-- The interface path you wrote            --<serviceClass> xfiretest.userservice        </serviceClass>        <!--implementation Classpath-        <implementationClass>            xfireTest.xfireTestImp.UserServiceImp        </implementationclass >        </service>

Then the code that the client calls:
Package Test;import Org.codehaus.xfire.xfirefactory;import Org.codehaus.xfire.client.xfireproxyfactory;import Org.codehaus.xfire.service.service;import Org.codehaus.xfire.service.binding.objectservicefactory;public Class CallWebServiceTest2 {public static void main (string[] args) {Service Srmodel = new Objectservicefactory (). Crea        Te (Userservice.class);        Xfireproxyfactory factory = new Xfireproxyfactory (xfirefactory. newinstance (). Getxfire ());//Create factory instance        String Hellourl = "Http://localhost:8082/xfireTest/services/UserService";            try {userservice service = (userservice) factory.create (Srmodel, Hellourl);            Usermodel user = new Usermodel ();            User.setage (22);            User.setusername ("test");        System.out.println (Service.adduser (user));        } catch (Exception e) {throw new RuntimeException (e); }    }}

Because it is a different project from the server, I need to create the same service interface as the server and the Usermodel class, which I write in the same package as the previous class, in test:
Interface:
Package Test;import Javax.jws.webparam;import Javax.jws.WebService; @WebService (serviceName = "UserService", targetnamespace = "Http://xfireTest/UserService") public interface UserService {public    Usermodel AddUser (@ Webparam (name = "user") Usermodel user);

Usermodel class:
Package Test;import Java.io.serializable;public class Usermodel implements Serializable {    /**     *    */ Private static final long serialversionuid = 9024481738536854407L;    Public Usermodel () {        super ();    }    Public Usermodel (String userName, int.) {        super ();        This.username = UserName;        This.age = age;    }    /**     * User name */    private String userName;    /**     * User age */    private int;    Public String GetUserName () {        return userName;    }    public void Setusername (String userName) {        this.username = userName;    }    public int getage () {        return age;    }    public void Setage (int.) {        this.age = age;    }    @Override public    String toString () {        return ' Usermodel [username= "+ UserName +", age= "+ Age +"] ";    }}

Above the code and previous comparisons, except add some annotations, basically not much difference, and then start the server, and in the simulation of the client call, the results console printing information is not expected, has become the default value,

So I looked up a lot of information, and the project boss's help to find the reason. Unexpectedly because of my client's Usermodel class package name and service side of the different, so a new and server-side package, and the Usermodel to move over, and then start the main method of the simulation client, the output of the console's wish, so the entire test is complete.







Java program calls Xfire published WebService service (II)

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.