First, create a Web Service Project:
After you click Finish, myeclipse will automatically generate services for you. XML and Web application directory structure, where services. XML is the configuration file you export the service, note in WEB-INF/web. xfire servlet is configured in the XML file.
<Servlet>
<Servlet-Name> xfireservlet </servlet-Name>
<Servlet-class> org. codehaus. xfire. Transport. http. xfirepolicableservlet </servlet-class>
<Load-on-startup> 0 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-Name> xfireservlet </servlet-Name>
<URL-pattern>/services/* </url-pattern>
</Servlet-mapping>
2. Now, write the pojo class to be exported. The first is the interface:
Package net. rubyeye. webservicedemo;
// Generated by myeclipse
Public interface ihelloworldservice {
Public String sayhello (string name );
}
This interface only provides one method: sayhello (). We do not use the jsr181 annotation declaration method or xml configuration file. Then there is the implementation class:
Package net. rubyeye. webservicedemo;
// Generated by myeclipse
Public class helloworldserviceimpl implements ihelloworldservice {
Public String sayhello (string name ){
Return "hello," + name;
}
}
Finally, configure the services. xml file:
<Service>
<Name> helloworldservice </Name>
<Serviceclass>
Net. rubyeye. webservicedemo. ihelloworldservice
</Serviceclass>
<Implementationclass>
Net. rubyeye. webservicedemo. helloworldserviceimpl
</Implementationclass>
<Style> wrapped </style>
<Use> literal </use>
<Scope> application </scope>
</Service>
Our web service name is helloworldservice, the interface is ihelloworldservice, and the Implementation class is helloworldserviceimpl.Note that the three steps can be completed in one step. You only need to use the new Web Service Wizard of myeclipse.
3. Then deploy the project to Tomcat through http: // localhost: 8081/helloworld/services/helloworldservice? The generated WSDL file is displayed in the WSDL file. Note that after deployment, services will be copied to the WEB-INF \ Classes \ META-INF \ xfire directory, xfire will automatically search for this directory and load the configuration file. You can write a client to test the web service, or click launch the Web Services on myeclipse to test the web service.
4. Compile the client code:
Package net. rubyeye. webservicedemo;
Import java.net. malformedurlexception;
Import java. util. arraylist;
Import java. util. List;
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 helloworldclient {
Public static void main (string ARGs []) {
Service srvcmodel = new objectservicefactory ()
. Create (ihelloworldservice. Class );
Xfireproxyfactory factory = new xfireproxyfactory (xfirefactory
. Newinstance (). getxfire ());
String helloworldurl = "http: // localhost: 8081/helloworld/services/helloworldservice ";
Try {
Ihelloworldservice srvc = (ihelloworldservice) Factory. Create (
Srvcmodel, helloworldurl );
System. Out. Print (srvc. sayhello ("Dennis "));
} Catch (malformedurlexception e ){
E. printstacktrace ();
}
}
}
Run and print: Hello, Dennis
Note: You can also use the new WebService client Wizard of myeclipse to automatically generate the client and the stub class for the client to call.
Finally, let's write another example of C # calling this web service.
1. Create a new windows application in vs.netProgramProject, and add a button and a label
2. Add a web application from the project menu -->, enter the URL of the Web Service's WSDL file to be called, and click go.
3. After a reference is added, vs.net will automatically generate stub for the client to call. These files are in the namespace named localhost. There will be a class named helloworldservice in this space. Finally, add the code to the onclick event of button1:
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Namespace myproject
{
Public partial class form1: Form
{
Localhost. helloworldservice helloservice = new localhost. helloworldservice ();
Public form1 ()
{
Initializecomponent ();
}
Private void button#click (Object sender, eventargs E)
{
Label1.text = helloservice. sayhello ("Dennis ");
}
}
}
We create a new helloworldservice and call the sayhello method to display the result on the label.
4. Run CTR + F5