Getting started with xfire WebService Development

Source: Internet
Author: User

First, create a WebService project in eclipse as the server, and write the following code:

Ihelloservice. Java service interface, there is a method, parameters and return values are complex types of user

 

Package test;

Public interface ihelloservice ...{
Public user getuser (User user );
}

 

User. Java

Note: this JavaBean must have a constructor without parameters by default. Otherwise, an exception that cannot instantiate the user object will occur during Aegis binding.

Package test;

Import java. Io. serializable;

Public class user ...{
Private string username;
Private string password;
Public user ()...{

}
Public user (string username, string password )...{
Super ();
This. Username = username;
This. Password = password;
}
Public String GetPassword ()...{
Return password;
}
Public void setpassword (string password )...{
This. Password = password;
}
Public String GetUserName ()...{
Return username;
}
Public void setusername (string username )...{
This. Username = username;
}
}

Helloserviceimpl. Java

The method is to pass in a user object, change the username and password of this object to the value we set, and then return this object.

Package test;

Public class helloserviceimpl implements ihelloservice ...{

Public void print ()...{
System. Out. println ("action ");

}

Public user getuser (User user )...{

User. setusername ("new name ");
User. setpassword ("new password ");
Return user;
}

}

Ihelloservice. Aegis. XML is bound to the return type for complex parameters. It is in the same package as ihelloservice.

 

<? XML version = "1.0" encoding = "UTF-8"?>

<Mappings>
<Mapping>
<Method name = "getuser">
<Parameter index = "0" componenttype = "test. User"/>
<Return-type componenttype = "test. User"/>

</Method>


</Mapping>
</Mappings>


Services. xml xfire release file

 

<? XML version = "1.0" encoding = "UTF-8"?>


<Beans>
<Service xmlns = "http://xfire.codehaus.org/config/1.0">
<Name> helloservice </Name>
<Namespace> http: // test/helloservice </namespace>
<Serviceclass> test. ihelloservice </serviceclass>
<Implementationclass> test. helloserviceimpl </implementationclass>

</Service>
</Beans>

 

Deploy it to Tomcat and run http: // localhost: 8080/xfire/services/helloservice in the browser? WSDL

If the deployment is correct, the WSDL file generated by xfire is displayed.

 

Package ihelloservice. Java, ihelloservice. Aegis. XM, and user. Java into jar

Next, we will write the clients that consume ws.

 

Create a Java project and put the jar of the server in classpath. Of course, both the service and the client must have the xfire class library.

Write code

 

Package test;
Import java.net. malformedurlexception;

Import org. codehaus. xfire. Client. xfireproxyfactory;
Import org. codehaus. xfire. Service. Service;
Import org. codehaus. xfire. Service. Binding. objectservicefactory;

Import test. ihelloservice;
Import test. user;





Public class client ...{

/***//**
* @ Param ARGs
*/
Public static void main (string [] ARGs )...{
User user = new user ("2", "2 ");
String serviceurl = "http: /localhost: 8080/xfire/services/helloservice ";
Service servicemodel = new objectservicefactory (). Create (ihelloservice. Class, null, "http: // test/helloservice", null );
Xfireproxyfactory servicefactory = new xfireproxyfactory ();
Ihelloservice service = NULL;
Try ...{
Service = (ihelloservice) servicefactory. Create (servicemodel, serviceurl );

User = service. getuser (User );

System. Out. println (user. GetUserName () + "-" + User. GetPassword ());
} Catch (malformedurlexception e )...{

E. printstacktrace ();
}
}

}

 

Start tomcat in the bin in the installation directory of consumer A (be sure not to use the startup plug-in the eclipse environment, otherwise the service will be automatically disabled when running the client)

 

Running result:

New name-New Password

What we really want

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.