CXF发布webservice接口

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中更方便,思路更清晰,即在项目启动时能够加载到该配置即可,这种配置方式无论什么框架都可以这么配置,也不用担心公司对框架的封装,亲测可用。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值