基于Apache CXF的Web Service服务端/客户端

CXF服务端:

package com.sean.server;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface Plus {
	public int add(@WebParam(name="x") int x, @WebParam(name="y") int y);
}
package com.sean.server;

public class PlusImpl implements Plus {
	public int add(int x, int y){
		return x + y;
	}
}
package com.sean.server;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.frontend.ServerFactoryBean;

public class Server {
    public static void main(String args[]) throws Exception {
    	PlusImpl plusImpl = new PlusImpl();
    	
        JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
    	//ServerFactoryBean factory = new ServerFactoryBean();
        factory.setServiceClass(Plus.class);
        factory.setAddress("http://127.0.0.1:8888/Plus");
        factory.setServiceBean(plusImpl);
        factory.create();
    }
}

程序启动后,访问http://127.0.0.1:8888/Plus?wsdl即可查看自动生成的WSDL文件

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://server.sean.com/" 
		name="PlusService" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" 
		xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
		xmlns:tns="http://server.sean.com/" 
		xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
		xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
	<wsdl:types> 
		<xs:schema targetNamespace="http://server.sean.com/" 
				xmlns:tns="http://server.sean.com/" version="1.0" 
				elementFormDefault="unqualified" 
				xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
			<xs:element name="add" type="tns:add"/> 
			<xs:element name="addResponse" type="tns:addResponse"/> 
			<xs:complexType name="add"> 
				<xs:sequence> 
					<xs:element name="x" type="xs:int"/> 
					<xs:element name="y" type="xs:int"/> 
				</xs:sequence> 
			</xs:complexType> 
			<xs:complexType name="addResponse"> 
				<xs:sequence> 
					<xs:element name="return" type="xs:int"/> 
				</xs:sequence> 
			</xs:complexType> 
		</xs:schema> 
	</wsdl:types> 
	<wsdl:message name="addResponse"> 
		<wsdl:part name="parameters" element="tns:addResponse"> </wsdl:part> 
	</wsdl:message> 
	<wsdl:message name="add"> 
		<wsdl:part name="parameters" element="tns:add"> </wsdl:part> 
	</wsdl:message> 
	<wsdl:portType name="Plus"> 
		<wsdl:operation name="add"> 
			<wsdl:input name="add" message="tns:add"> </wsdl:input> 
			<wsdl:output name="addResponse" message="tns:addResponse"> </wsdl:output> 
		</wsdl:operation> 
	</wsdl:portType> 
	<wsdl:binding name="PlusServiceSoapBinding" type="tns:Plus"> 
		<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> 
		<wsdl:operation name="add"> 
			<soap:operation style="document" soapAction=""/> 
			<wsdl:input name="add"> 
				<soap:body use="literal"/> 
			</wsdl:input> 
			<wsdl:output name="addResponse"> 
				<soap:body use="literal"/> 
			</wsdl:output> 
		</wsdl:operation> 
	</wsdl:binding> 
	<wsdl:service name="PlusService"> 
		<wsdl:port name="PlusPort" binding="tns:PlusServiceSoapBinding"> 
			<soap:address location="http://127.0.0.1:8888/Plus"/> 
		</wsdl:port> 
	</wsdl:service> 
</wsdl:definitions>

如果服务端使用ServerFactoryBean类,则最终生成的WSDL文件略有不同
 

CXF客户端:

如果服务端使用ServerFactoryBean类,则客户端需要使用JaxWsServerFactoryBean类

如果服务端使用JaxWsServerFactoryBean类,则客户端需要使用JaxWsProxyFactoryBean类

package com.sean.client;

import org.apache.cxf.frontend.ClientProxyFactoryBean;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.sean.server.Plus;

public class Client {
	public static void main(String[] args) {
		//ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress("http://127.0.0.1:8888/Plus");
        Plus client = factory.create(Plus.class);
        System.out.println(client.add(2, 2));
        System.exit(0);
	}
}

无论服务端使用ServerFactoryBean类还是JaxWsServerFactoryBean类,都可在客户端使用JaxWsDynamicClientFactory类,并通过反射的方式调用WebService服务端

package com.sean.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class Client2 {
	public static void main(String[] args) throws Exception {
        JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
        Client client = factory.createClient("http://127.0.0.1:8888/Plus?wsdl");
        Object[] inputs = {1, 2};
        Object[] result = client.invoke("add", inputs);
        System.out.println(result[0]);
	}
}

 

你可能感兴趣的:(apache,webservice,CXF)