前言
项目需要发布成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包少所以一直没有成功,多查、多尝试。