SSM下使用cxf发布webservice接口
引入jar包,我用的是pom文件
<!--cxf-->
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-core -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
创建webservice接口
package com.test.testWebService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
/**
* @Title:
* @author TODO
* @since JDK1.6
* @history 2019年1月17日 TODO 新建
*/
@WebService
public interface ChaoTuDuiJieService {
public String message(String param);
}
创建接口的实现类
package com.test.testWebServiceImpl;
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service("helloWorld")
@WebService
public class ChaoTuDuiJieServiceImpl implements ChaoTuDuiJieService{
@Override
public String message(String param){
System.out.println("测试webservice接口");
}
}
创建cxf-servlet.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: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:server id="testService" serviceClass="com.test.testWebService.ChaoTuDuiJieService" address="/testService">
<jaxws:serviceBean>
<bean class="com.test.testWebService.ChaoTuDuiJieServiceImpl">
</bean>
</jaxws:serviceBean>
</jaxws:server>
</beans>
将cxf-servlet.xml文件配置到web.xml中,使项目启动时立即加载该文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:conf/spring-mybatis.xml;
classpath:conf/platform/spring-base-platform.xml;
/WEB-INF/cxf-servlet.xml;
</param-value>
</context-param>
<!-- cxf的核心类,访问的路径在发布接口时要用 -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
此时访问http://127.0.0.1:8099/xmgl/webservice/testService?wsdl即可查看发布的webservice接口
客户端代码生成
在cmd窗口中输入命令行
wsimport -keep -d E:\eclipse\Testproject\src http://127.0.0.1:8099/xmgl/webservice/testService?wsdl
最好是将路径写成项目中该代码块所在的路径
调用代码示例
URL wsdlURL = ProcessServiceImplService.WSDL_LOCATION;
ProcessServiceImplService ss = new ProcessServiceImplService(wsdlURL, PROCESS_SERVICE_NAME);
ProcessService port = ss.getProcessServiceImplPort();
port.方法();
问题说明:
我在配置cxf.xml和 cxf-servlet.xml时,起初不是配置在cxf-servlet.xml中的,而是配置在了spring的配置文件中,但是一直引入失败,未找到原因,后经同事指正,发现是spring的配置文件被公司封装后修改了命名空间,所以引入此两个xml文件时需要在引入的标签上加上该命名空间;
个人感觉配置在cxf-servlet.xml中更方便,思路更清晰,即在项目启动时能够加载到该配置即可,这种配置方式无论什么框架都可以这么配置,也不用担心公司对框架的封装,亲测可用。