WEB项目 CXF与Spring整合服务端和客户端创建

一、服务器端

     1、添加服务端所需要的jar

          cxf-2.2.jar
          wsdl4j-1.6.2.jar
          xml-resolver-1.2.jar
          XmlSchema-1.4.2.jar

        2、Web.xml添加servlet

	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/wservice/*</url-pattern>
	</servlet-mapping>

    3、添加配置文件

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

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />          <!--必须引入>
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />   <!--必须引入-->
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />     <!--必须引入-->
    <bean id="kltrustService" class="com.webservice.TestService"/>      <!--实现类-->
    <jaxws:server id="iKltrustService" serviceClass="com.webservice.ITestService" address="/kltrust">   <!--服务接入口及地址-->
		<jaxws:serviceBean>
			<ref bean="kltrustService"/>
		</jaxws:serviceBean>
	</jaxws:server>
</beans>

4、实现类

@WebService      //
public class TestService implements ITestService {
	 //测试
	public String test(String userName,String parameter){
	       return "Hello " + userName + ", currentTime is " + Calendar.getInstance().getTime(); 
	   } 
}
完成启动服务      浏览器端输入地址   http://localhost:8090/MyPro/wservice/kltrust?wsdl        //http://localhost:8090/MyPro 为我的web项目的地址

显示XML文件即发布成功 ,可能浏览器设置问题不显示可查看源码


二、 客户端编写

为了方便测试我新建了一个java项目导入jar包

cxf-2.2.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-jaxws_2.1_spec-1.0.jar
geronimo-jms_1.1_spec-1.1.1.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.2.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.9.jar
jaxb-xjc-2.1.9.jar
neethi-2.0.4.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.6.jar
xml-resolver-1.2.jar
XmlSchema-1.4.4.jar

                DynamicClientFactory dcf = DynamicClientFactory.newInstance ();             
		String url = "http://localhost:8090/MyPro/wservice/kltrust?wsdl";
		Client client = dcf.createClient(url);       
		String param = "{\"tokenId\":\"402881e44689a61f014689a9ba350003\",\"taskId\":\"7300512\",\"method\":\"getTaskDetail\"}";   //参数构造
		Object object=new Object[]{param};
		//调用此方法
		Object[] results = client.invoke("test", new Object[] {param,"{\"aaaaaaaa\"}"});  \\test为服务端的方法名称  第二个参数为test的参数可以
//为多个参数数组依次排序 返回为服务端该方法的返回值
		System.out.print(results[0].toString());   
完成客户端调动 

暂时满足项目需要,待深入研究

你可能感兴趣的:(WEB项目 CXF与Spring整合服务端和客户端创建)