周末學(xué)習(xí)了cxf發(fā)布webservice的例子,按照官網(wǎng)上的步驟進(jìn)行配置,錯(cuò)誤不斷,經(jīng)過(guò)一番折騰,終于成功了。
下面說(shuō)說(shuō)我的配置:
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服務(wù)啟動(dòng)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> <!--日志攔截功能,用于監(jiān)控soap內(nèi)容,開(kāi)發(fā)后可以刪除 --> <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); }
實(shí)現(xiàn):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下面測(cè)試過(guò)有異常
6.添加測(cè)試類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代替
結(jié)束。
代碼打包,提供下載,lib包自行下載,提供網(wǎng)址:http://jarvana.com/jarvana/
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
