前言
項目需要釋出成webservice的接口。
準備工作
1.引入apache-cxf 3.2.0jar包,下載下傳位址http://cxf.apache.org/download.html
2.web.xml添加配置
<!-- 配置cxf的核心控制器 -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- 所有來自/webservice/*的請求交給cxf處理 -->
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
3.添加cxf配置檔案

4.spring-dao中添加引用
<import resource="classpath:spring/apache-cxf.xml"/>
5.例子
service
package com.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface WebserviceTest {
@WebMethod
public String sayHello(@WebParam(name="param") String param);
}
service.impl
import javax.jws.WebService;
import org.springframework.stereotype.Component;
import com.service.WebserviceTest;
@Component
@WebService(endpointInterface= "com.service.WebserviceTest")
public class WebserviceTestImpl implements WebserviceTest {
public String sayHello(String param) {
System.out.println("已經jaxws:endpoint調用成功========");
return "Hello world!!!我是"+param;
}
}
apache-cxf配置檔案
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!--CXF配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<!--
釋出webservice應用程式
serviceClass屬性 : 指向目前需要釋出的webservice應用實作的接口
address屬性 : 描述目前釋出的webservice程式唯一位址
當其他工程需要通路本工程釋出的某一個webservcie程式時,需要通過這個唯一位址進行通路
serviceBean : 指向目前釋出的webservice程式的具體實作類
-->
<!-- 通過jaxws:endpoint方式來配置webservice -->
<jaxws:endpoint id="webservice" implementor="com.service.impl.WebserviceTestImpl"
address="/hello" />
</beans>
6.啟動tomcat
輸入網址:http://localhost:8080/ssm/webservice//hello?WSDL
顯示如下界面表示成功
總結
其實看起來不難但是一開始引入的jar包少是以一直沒有成功,多查、多嘗試。