XFire builds the web service client in five ways

  • 2020-06-01 09:50:13
  • OfStack

The application of JSR 181 Annotations is not covered here. The three ways are as follows

Create a dynamic client via the WSDL address
Through the interface provided by the server to create a client
Use Ant to generate the client through the WSDL file

The first way: create a dynamic client with the WSDL address


package com.jadyer.client; 
import java.net.MalformedURLException; 
import java.net.URL; 
import org.codehaus.xfire.client.Client; 
/** 
 *  through WSDL To create a dynamic client  
 * @see  This is when you need to introduce it into your project XFire 1.2 Core Libraries and XFire 1.2 HTTP Client Libraries 
 */ 
public class ClientFromWSDL { 
 public static void main(String[] args) throws MalformedURLException, Exception { 
 Client client = new Client(new URL("http://127.0.0.1:8080/XFire_demo/services/XFireServer?wsdl")); 
 Object[] results11 = client.invoke("sayHello", new Object[]{"Jadyer22"}); 
 System.out.println(results11[0]); 
 } 
} 

The second way: create a client through a port provided by the server


package com.jadyer.client; 
import java.net.MalformedURLException; 
import java.util.List; 
import org.codehaus.xfire.client.XFireProxyFactory; 
import org.codehaus.xfire.service.Service; 
import org.codehaus.xfire.service.binding.ObjectServiceFactory; 
import com.jadyer.model.Person; 
import com.jadyer.model.User; 
import com.jadyer.server.HelloService; 
/** 
 *  through Web The server provides the interface to create the client  
 * @see  The client must provide 1 And the server side completely 1 To the interface, also want the package name 1 to  
 * @see  In this case, on the client side ( Is the project ) Provided in the HelloService.java Interfaces, and Person and User two POJO class  
 * @see  And it needs to be introduced into the project at this point XFire 1.2 Core Libraries and XFire 1.2 HTTP Client Libraries 
 */ 
public class ClientFromInterface { 
 public static void main(String[] args)throws MalformedURLException{ 
 // The first to use XFire the ObjectServiceFactory from HelloService Interface to create 1 Service model serviceModel 
 //serviceModel Contains a description of the service, in other words, the metadata of the service  
 //Create a metadata of the service 
 Service serviceModel = new ObjectServiceFactory().create(HelloService.class); 
 // Access address  
 String serviceURL = "http://127.0.0.1:8080/XFire_demo/services/XFireServer"; 
 // By looking at the org.codehaus.xfire.client.XFireProxyFactory The source code found  
 // The next two lines of code are direct from here new XFireProxyFactory() The effect is equivalent  
 //XFire xfire = XFireFactory.newInstance().getXFire(); 
 //XFireProxyFactory factory = new XFireProxyFactory(xfire); 
 // for XFire To obtain 1 A proxy factory object  
 //Create a proxy for the deployed service 
 XFireProxyFactory factory = new XFireProxyFactory(); 
 // through proxyFactory , using the service model serviceModel And service endpoints URL( Used to get WSDL) 
 // get 1 The local agent of the service, which is the actual client  
 HelloService client = (HelloService)factory.create(serviceModel, serviceURL); 
 /** 
  * Invoke the service 
  * @see  Invoke the local proxy of the service ( The actual client ) We get what we need WebServcie 
  */ 
 /*-- Handling simple objects --*/ 
 String serviceResponse = client.sayHello("Jadyer11"); 
 System.out.println(serviceResponse); 
 /*-- Handle object --*/ 
 User u = new User(); 
 u.setName("Jadyer99"); 
 Person pp = client.getPerson(u); 
 System.out.println(pp.getName()); 
 /*-- To deal with List--*/ 
 List<Person> personList = client.getPersonList(24, "Jadyer88"); 
 for(Person p : personList){ 
  System.out.println(p.getName()); 
 } 
 } 
} 

This is the interface it will use and the two POJO classes


/** 
 * Web The service provides an interface to the client  
 * @see  This is the first 2 The way to create the client side, to use the interface  
 */ 
package com.jadyer.server; 
import java.util.List; 
import com.jadyer.model.Person; 
import com.jadyer.model.User; 
public interface HelloService { 
 public String sayHello(String name); 
 public Person getPerson(User u); 
 public List<Person> getPersonList(Integer age, String name); 
} 
/** 
 *  The first 2 The way to create a client is to use two POJO class  
 */ 
package com.jadyer.model; 
public class User { 
 private String name; 
 /*--getter and setter slightly --*/ 
} 
package com.jadyer.model; 
public class Person { 
 private Integer age; 
 private String name; 
 /*--getter and setter slightly --*/ 
} 

The third way: use Ant to generate clients through WSDL files


package com.jadyer.client; 
/** 
 *  use Ant through WSDL Generate client  
 * @see  Here, ClientFromAnt.java I built it myself, not really Ant generate  
 * @see  That's what we're going to use here JAR There are: xfire-all-1.2.6.jar As well as //xfire-distribution-1.2.6//lib// All in the directory JAR package  
 * @see  We need to take these JAR Copy all the packages Web Project//WebRoot//WEB-INF//lib// In the directory  
 * @see  Then put the build.xml and MyFirstXFireServer.wsdl Copy everything down Web Project The root directory can be  
 * @see  about MyFirstXFireServer.wsdl The file is mine WebServices After the service starts  
 * @see  access http://127.0.0.1:8080/XFire_demo/services/XFireServer?wsdl And then I'm going to save it  
 */ 
public class ClientFromAnt { 
 public static void main(String[] args) { 
 XFireServerClient client = new XFireServerClient(); 
 //String url = "http://127.0.0.1:8080/XFire_demo/services/XFireServer"; 
 //String result = client.getXFireServerHttpPort(url).sayHello("Jadyer33"); 
 // The two lines of code above, and the one below 1 Line of code, same effect ~~ 
 String result = client.getXFireServerHttpPort().sayHello("Jadyer33"); 
 System.out.println(result); 
 } 
} 

The Ant file used is as follows


<?xml version="1.0" encoding="UTF-8"?> 
<project name="wsgen" default="wsgen" basedir="."> 
 <path id="classpathId"> 
 <fileset dir="./WebRoot/WEB-INF/lib"> 
  <include name="*.jar" /> 
 </fileset> 
 </path> 
 <taskdef classpathref="classpathId" name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask"/> 
 <target name="wsgen" description="generate client"> 
 <wsgen outputDirectory="./src/" wsdl="MyFirstXFireServer.wsdl" binding="xmlbeans" package="com.jadyer.client" overwrite="true"/> 
 </target> 
</project> 

You can also use the Ant file below


<?xml version="1.0" encoding="UTF-8"?> 
<project name="xfireAnt" basedir="." default="createClientCode"> 
 <property name="xfirelib" value="${basedir}/WebRoot/WEB-INF/lib"/> 
 <property name="sources" value="${basedir}/src"/> 
 <path id="classpath"> 
 <fileset dir="${xfirelib}"> 
  <include name="*.jar"/> 
 </fileset> 
 </path> 
 <target name="createClientCode"> 
 <taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="classpath"/> 
 <wsgen outputDirectory="${sources}" wsdl="http://127.0.0.1:8080/XFire_demo/services/XFireServer?wsdl" package="com.jadyer.client" overwrite="true"/> 
 </target> 
</project> 

Finally, I will add the content of MyFirstXFireServer.wsdl


<?xml version="1.0" encoding="UTF-8"?> 
<wsdl:definitions targetNamespace="http://www.jadyer.com/XFireDemo" 
xmlns:tns="http://www.jadyer.com/XFireDemo" 
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" 
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" 
xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" 
 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 
 <wsdl:types> 
 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
   attributeFormDefault="qualified" 
   elementFormDefault="qualified" 
targetNamespace="http://www.jadyer.com/XFireDemo"> 
  <xsd:element name="sayHello"> 
  <xsd:complexType> 
   <xsd:sequence> 
   <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string" /> 
   </xsd:sequence> 
  </xsd:complexType> 
  </xsd:element> 
  <xsd:element name="sayHelloResponse"> 
  <xsd:complexType> 
   <xsd:sequence> 
   <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" /> 
   </xsd:sequence> 
  </xsd:complexType> 
  </xsd:element> 
 </xsd:schema> 
 </wsdl:types> 
 <wsdl:message name="sayHelloRequest"> 
 <wsdl:part name="parameters" element="tns:sayHello"></wsdl:part> 
 </wsdl:message> 
 <wsdl:message name="sayHelloResponse"> 
 <wsdl:part name="parameters" element="tns:sayHelloResponse"></wsdl:part> 
 </wsdl:message> 
 <wsdl:portType name="XFireServerPortType"> 
 <wsdl:operation name="sayHello"> 
  <wsdl:input name="sayHelloRequest" message="tns:sayHelloRequest"> 
  </wsdl:input> 
  <wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse"> 
  </wsdl:output> 
 </wsdl:operation> 
 </wsdl:portType> 
 <wsdl:binding name="XFireServerHttpBinding" type="tns:XFireServerPortType"> 
 <wsdlsoap:binding style="document" mce_style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
 <wsdl:operation name="sayHello"> 
  <wsdlsoap:operation soapAction="" /> 
  <wsdl:input name="sayHelloRequest"> 
  <wsdlsoap:body use="literal" /> 
  </wsdl:input> 
  <wsdl:output name="sayHelloResponse"> 
  <wsdlsoap:body use="literal" /> 
  </wsdl:output> 
 </wsdl:operation> 
 </wsdl:binding> 
 <wsdl:service name="XFireServer"> 
 <wsdl:port name="XFireServerHttpPort" binding="tns:XFireServerHttpBinding"> 
  <wsdlsoap:address location="http://127.0.0.1:8080/XFire_demo/services/XFireServer" /> 
 </wsdl:port> 
 </wsdl:service> 
</wsdl:definitions> 

The fourth way

This method USES the jar package of spring, which I saw a few days ago when I was looking for the information of XFire+Spring, and I will also make a record here. Again, both this method and the second method mentioned above require the same interface to the server on the client side as well as the same package name.

(1) create client.xml in the src directory (name is not specific)


<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
 <bean id="baseService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean" lazy-init="false" abstract="true"/>
<!-- id Is used as an identifier in the client program service If there are more than one service How to add more below bean Can be -->
 <bean id="MathService" parent="baseService">
 <property name="serviceClass">
 <value>service.MathService</value>
 </property>
 <property name="wsdlDocumentUrl">
<value>http://localhost:8080/myservice/mathWebService?wsdl</value>
 </property>
 </bean>
</beans> 

(2) it is very simple to invoke the service code in the program


ApplicationContext ctx = new ClassPathXmlApplicationContext("client.xml");
MathService mathService = (MathService)ctx.getBean("MathService");
int result = mathService.add(int one,int two);

The fifth way

Get the wsdl file first, name it mathWebService.wsdl and put it in the src directory of the client, then access the wsdl file programmatically and call the required method.


 String wsdl = "mathWebService.wsdl " ; //  The corresponding WSDL file  
 Resource resource = new ClassPathResource(wsdl); 
 Client client = new Client(resource.getInputStream(), null ); //  According to the WSDL Creating a customer instance  
 Object[] objArray = new Object[ 2 ];
 objArray[ 0 ] = 2 ;
 obiArray[1] = 3;
  //  Call specific Web Service methods  
 Object[] results = client.invoke( " add " , objArray);
 System.out.println( " result: " + results[ 0 ]);

Several methods for this, if 1 methods parameters passed as server-side entity object, this seems to be more troublesome, I don't know set up on the client and server of the same entity class line not line, no practice, return the result if is complex data types do not know to have what problem, or how to conversion, there is no in-depth study. And I personally don't think method calls are that intuitive.


Related articles: