CXF2.4 Spring3 编写WebService Server & Client

最近由于公司的项目需求,需要编写WebService,以前用axis2,领导讲有问题,具体我还不了解到底出了什么问题,让我改成Xfire,xfire 被 CXF 并购,并且 有apache来进行维护和升级,xfire 最后一次更新版本是1.2.6,本案例讲的是cxf和spring集成

需要准备的jar文件有:
commons-logging-1.1.1.jar
cxf-2.4.7.jar
geronimo-activation_1.1_spec-1.1.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-javamail_1.4_spec-1.7.1.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
jaxb-api-2.2.3.jar
jaxb-impl-2.2.4-1.jar
neethi-3.0.2.jar
stax2-api-3.1.1.jar
woodstox-core-asl-4.1.2.jar
wsdl4j-1.6.2.jar
xmlschema-core-2.0.2.jar
geronimo-servlet_2.5_spec-1.1.2.jar
geronimo-ws-metadata_2.0_spec-1.1.3.jar
saaj-api-1.3.4.jar
saaj-impl-1.3.12.jar
wss4j-1.6.5.jar
xml-resolver-1.2.jar
aopalliance-1.0.jar
spring-beans-3.0.6.RELEASE.jar
spring-context-3.0.6.RELEASE.jar
spring-core-3.0.6.RELEASE.jar
spring-web-3.0.6.RELEASE.jar
asm-3.3.1.jar
slf4j-api-1.6.2.jar
slf4j-jdk14-1.6.2.jar
geronimo-jaxws_2.2_spec-1.1.jar
spring-asm-3.0.6.RELEASE.jar
spring-expression-3.0.6.RELEASE.jar
spring-aop-3.0.6.RELEASE.jar

我们 写好 接口和 实现类,配置在 spring bean。
内容如下:
接口:
package demo.spring.service;

import java.util.List;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
	
	String sayHi(String text);
	
	List<String> getList(List<String> list);
}

实现类:
package demo.spring.service;

import java.util.List;
import javax.jws.WebService;

@WebService(endpointInterface = "demo.spring.service.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    public String sayHi(String text) {
        return "Hello " + text;
    }

	public List<String> getList(List<String> list) {
		return list;
	}
}

spring配置文件:
<?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.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="demo.spring.service.HelloWorldImpl"  address="/HelloWorld" />

</beans>


web.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	
   <context-param>
   	<param-name>contextConfigLocation</param-name>
   	<param-value>WEB-INF/applicationContext-cxf.xml</param-value>
   </context-param>

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

    <servlet>
        <servlet-name>cxf</servlet-name>
        <display-name>cxf</display-name>
        <description>Apache CXF Endpoint</description>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

   <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>



启动工程,访问地址:http://ip:端口/项目名字/services/HelloWorld?wsdl
客户端分 静态调用和动态调用,静态我用spring来集成。
spring文件:
<?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.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">

    <bean id="client" class="demo.spring.service.HelloWorld" 
      factory-bean="clientFactory" factory-method="create"/>
    
	<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
	  <property name="serviceClass" value="demo.spring.service.HelloWorld"/>
	  <property name="address" value="http://localhost:9000/Cxf/services/HelloWorld?wsdl"/>
	</bean>
	  
</beans>

客户端 静态调用程序程序:
package demo.spring.client;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import demo.spring.service.HelloWorld;

public final class StaticTest {

    public static void main(String args[]) throws Exception {
        ClassPathXmlApplicationContext context   = new ClassPathXmlApplicationContext("client-beans.xml");
        HelloWorld client = (HelloWorld)context.getBean("client");
        String response = client.sayHi("Joe");
        System.out.println("Response: " + response);
        System.exit(0);
    }
}


客户端动态调用代码:
package demo.spring.client;

import java.util.ArrayList;
import java.util.List;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;


public class DynamicSimple {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {

		JaxWsDynamicClientFactory dynamicClient = JaxWsDynamicClientFactory.newInstance();
		Client client = dynamicClient.createClient("http://localhost:9000/Cxf/services/HelloWorld?wsdl");
		try {
			List<String> list = new ArrayList<String>();
			list.add("iteye");
			list.add("kaobian");
			Object[] rspArr = client.invoke("getList",list);
			
			if (null != rspArr && rspArr.length > 0) {
				for (int i = 0; i < rspArr.length; i++) {
					List<String> temp = (List<String>)rspArr[i];
					for(String s : temp ){
						System.out.println(s);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(java,spring,webservice)