CXF总结

CXF总结

如何来用cxf结合spring开发webservice接口。by@wangkun

下载cxf

建两个java web project并引入cxf相关jar包

webservice服务端:

webservice客户端:

webService服务端

开始,写一个service interface

	package com.webservice.server.wangkun;
	
	/**
	 * Created by wangkun on 2015/9/21.
	 */
	import javax.jws.WebService;
	
	@WebService
	public interface HelloWorld {
	    String sayHi(String text);
	}

接着,写我们的实现类:

	package com.webservice.server.wangkun;
	
	/**
	 * Created by wangkun on 2015/9/21.
	 */
	import javax.jws.WebService;
	
	@WebService(endpointInterface = "com.webservice.server.wangkun.HelloWorld")
	public class HelloWorldImpl implements HelloWorld {
	
	    public String sayHi(String text) {
	        System.out.println("sayHi called");
	        return "Hello " + text;
	    }
	}

声明server beans

CXF包含在Spring的“好XML”的支持。对于JAX-WS,建一个 jaxws:endpoint bean服务器端的endpoint。
在WEB-INF目录下的CXF-servlet.xml文件:

	<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.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-servlet.xml"/>
	    <jaxws:endpoint id="helloWorld" implementor="com.webservice.server.wangkun.HelloWorldImpl" address="/HelloWorld"/>
	</beans>

如果你想弹性的引用bean可以这样写:

	<bean id="hello" class="demo.spring.service.HelloWorldImpl" />
	<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
  • ID指定Spring上下文的bean的ID。
  • implementor是指实现类
  • address 是指请求路径

设置Servlet(web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/cxf-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/webService/*</url-pattern>
    </servlet-mapping>
</web-app>

访问

  • 部署到tomcat
  • 访问http://localhost:8080/webService/HelloWorld?wsdl,如果打开能看到wsdl文档,说明服务端已经发布成功。

webService客户端

利用cxf的生成工具生成客户端相关类

  1. 把cxf的可执行目录配置到系统环境变量下:

  2. 打开cmd,cd到wsdl文档所在的目录(该wsdl文档是服务端所产生的),执行命令
    wsdl2java wsdl2java F:webservice_projectcxfClientsrcHelloWorld.wsdl

  3. 效果,可以看到生成的客户端代码

像endpoint用于服务端一样,在客户端使用client,创建client-beans.xml文件

	<?xml version="1.0" encoding="UTF-8"?>
	<beans xmlns="http://www.springframework.org/schema/beans"
	       xmlns:jaxws="http://cxf.apache.org/jaxws"
	       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	       xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
	
	    <!--一种方式-->
	    <bean id="helloClient" class="com.webservice.server.wangkun.HelloWorld"
	          factory-bean="clientFactory" factory-method="create"/>
	    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
	        <property name="serviceClass" value="com.webservice.server.wangkun.HelloWorld"/>
	        <property name="address" value="http://localhost:8080/webService/HelloWorld"/>
	    </bean>
	
	    <!--另外一种jaxws:client方式 -->
	<!--    <jaxws:client id="helloClient"
	                  serviceClass="com.webservice.server.wangkun.HelloWorld"
	                  address="http://localhost:8080/webService/HelloWorld" />-->
	</beans>

编写测试类:

package com.webservice.test;

import com.webservice.server.wangkun.HelloWorld;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public final class Client {
    private Client() {
    }
    public static void main(String args[]) throws Exception {
        // START SNIPPET: client
        ClassPathXmlApplicationContext context 
            = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});
        HelloWorld client = (HelloWorld)context.getBean("client");
        String response = client.sayHi("Joe");
        System.out.println("Response: " + response);
        System.exit(0);
        // END SNIPPET: client
    }
}

测试结果

如果出现如下结果,说明客户端测试成功。

如果需要源码请加qq:398121505 或者给我留言,谢谢!

原文地址:https://www.cnblogs.com/kunpengit/p/4826503.html