周末学习了cxf发布webservice的例子,按照官网上的步骤进行配置,错误不断,经过一番折腾,终于成功了。
下面说说我的配置:
1.下载jar:
2.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!--
spring需要加载的配置文件
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:com/zhengs/spring-cxf.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--
cxf服务启动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>/service/*</url-pattern>
</servlet-mapping>
</web-app>
3.spring-cxf.xml
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:wsa="http://cxf.apache.org/ws/addressing"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.1.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<cxf:bus>
<cxf:features>
<!--日志拦截功能,用于监控soap内容,开发后可以删除 -->
<cxf:logging/>
<wsa:addressing/>
</cxf:features>
</cxf:bus>
<bean id="hello" class="com.zhengs.HelloWorldImpl" />
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" publish="true"/>
</beans>
4.webservice接口HelloWorld.java
package com.zhengs;
import javax.jws.WebService;
@WebService(targetNamespace="zhengs.com")
public interface HelloWorld {
String sayHi(String text);
}
实现:HelloWorldImpl.java
package com.zhengs;
import javax.jws.WebService;
@WebService(endpointInterface = "com.zhengs.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
return "Hello , " + text;
}
}
5加上log4j.properties文件,部署到tomcat6.0下面,在5.0.28下面测试过有异常
6.添加测试类Client.java
package com.zhengs.client;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zhengs.HelloWorld2;
public final class Client {
private Client() {
}
public static void main(String args[]) throws Exception {
// START SNIPPET: client
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext("beans.xml");
HelloWorld2 client = (HelloWorld2)context.getBean("helloClient");
String response = client.sayHi("Joe");
System.out.println("Response: " + response);
System.exit(0);
// END SNIPPET: client
}
}
HelloWorld2.java可以用HelloWorld.java代替
结束。
代码打包,提供下载,lib包自行下载,提供网址:http://jarvana.com/jarvana/

