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