首页

基于spring的jaxws:endpoint和客户端jaxws:client分别进行Apache的CXFWebService服务注册和调用完整示例

标签:apache,cxf,WebService,CXFServlet,cxf-rt-transports-http.jar,spring     发布时间:2017-03-18   

一、前言

基于Spring注册使用Apache的CXF注册WebService服务,通过简单的配置步骤即可注册和使用服务,无需再配置和本地生成很多中间冗余代码,易于开发及维护

二、示例步骤

1.发布服务步骤

注册接口类TradeXService

import java.util.List;@b@import javax.jws.WebMethod;@b@import javax.jws.WebService;@b@@b@import com.woopa.entity.TradeDetail;@b@@b@@WebService@b@public interface TradeXService {@b@	@b@	@WebMethod@b@	ResultDTO invoke(List<TradeDetail> dto);@b@@b@}

服务实现类TradeServiceImpl

import java.util.List;@b@import javax.jws.WebService;@b@import org.springframework.beans.factory.annotation.Autowired;@b@import org.springframework.stereotype.Service;@b@import com.woopa.common.persist.IPersistDao;@b@import com.woopa.entity.TradeDetail;@b@import com.woopa.service.ResultDTO; @b@import com.woopa.service.TradeXService;@b@@b@@b@/**http://localhost:9999/yoms/ws/tradeXService?wsdl*/@b@@Service@b@@WebService(endpointInterface = "com.woopa.service.TradeXService")@b@public class TradeServiceImpl implements  TradeXService {@b@	@b@	private IPersistDao dao;@b@	@b@	@b@	public IPersistDao getDao() {@b@		return dao;@b@	}@b@	@b@	@Autowired@b@	public void setDao(IPersistDao dao) {@b@		this.dao = dao;@b@	}@b@	@b@	@b@	@Override@b@	public ResultDTO invoke(List<TradeDetail> dto) {@b@		try {@b@			dao.saveAll(dto);@b@		} catch (Exception e) {@b@			e.printStackTrace();@b@			return new ResultDTO(500,"接受失败!");@b@		}@b@		return new ResultDTO(100,"接受成功!");@b@	}@b@}

相关Spring的jaxws:endpoint配置文件如下

<?xml version="1.0" encoding="UTF-8"?>@b@<beans xmlns="http://www.springframework.org/schema/beans"@b@    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"@b@    xmlns:jaxws="http://cxf.apache.org/jaxws"@b@    xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd @b@      					http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd"@b@    default-autowire="byName" default-lazy-init="true">@b@    @b@    <jaxws:endpoint id="tradeXService" address="/tradeXService" implementor="#tradeServiceImpl" />@b@   @b@</beans>

容器中web.xml注册cxf的servlet

<!-- CXF -->@b@	<servlet>@b@		<servlet-name>CXFServlet</servlet-name>@b@		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>@b@	</servlet>@b@	<servlet-mapping>@b@		<servlet-name>CXFServlet</servlet-name>@b@		<url-pattern>/ws/*</url-pattern>@b@	</servlet-mapping>

服务访问地址为http://localhost:9999/yoms/ws/tradeXService?wsdl,效果如下所示

基于spring的jaxws:endpoint和客户端jaxws:client分别进行Apache的CXFWebService服务注册和调用完整示例

2.客户端配置实现步骤

同样复制一份接口类TradeXService,如下

import java.util.List;@b@import javax.jws.WebMethod;@b@import javax.jws.WebService;@b@@b@import com.woopa.entity.TradeDetail;@b@@b@@WebService@b@public interface TradeXService {@b@	@b@	@WebMethod@b@	ResultDTO invoke(List<TradeDetail> dto);@b@@b@}

配置spring的jaxws:client文件,如下所示

<?xml version="1.0" encoding="UTF-8"?>@b@<beans xmlns="http://www.springframework.org/schema/beans"@b@       xmlns:jaxws="http://cxf.apache.org/jaxws"@b@       xmlns:http-conf="http://cxf.apache.org/transports/http/configuration" @b@       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"@b@       xsi:schemaLocation="@b@       	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd@b@		http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd@b@		http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd">@b@	@b@	<context:property-placeholder location="classpath:webservice.properties"/>@b@	@b@	<http-conf:conduit name="*.http-conduit">@b@    	<http-conf:client ConnectionTimeout="${ConnectionTimeout}" ReceiveTimeout="${ReceiveTimeout}"/>@b@    </http-conf:conduit>@b@        @b@    <jaxws:client id="tradeXService"@b@				  serviceClass="${ws.woopa.serviceClass.catlog}.TradeXService"@b@				  address="${ws.woopa.endPointAddress}tradeXService" /> @b@				  @b@</beans>

关联的webservice.properties配置内容如下

#endpoint address@b@ws.woopa.endPointAddress=http://localhost:9999/yoms/ws/@b@ws.woopa.serviceClass.catlog=com.woopa.service@b@ConnectionTimeout=30000@b@ReceiveTimeout=600000

客户端调用代码

@Controller@b@public class  TradeXAction extends BaseAction { @b@@b@	@Autowired@b@	private  TradeXService  tradexservice;@b@	@b@	public String remoteInvoke() throws Exception {  @b@		@b@		List<TradeDetail> dto =new ArrayList<TradeDetail>();@b@		@b@		TradeDetail td=new TradeDetail();@b@		@b@		td.setOrderId(5565656);@b@		td.setCustomerName("5555");@b@		td.setCustomerId("5555");@b@		td.setOrderName("55555");@b@		td.setShipperName("5555");@b@		@b@		dto.add(td);@b@		@b@		tradexservice.invoke(dto);@b@		@b@    }@b@    @b@ }

效果如下图所示,数据库插入成功

基于spring的jaxws:endpoint和客户端jaxws:client分别进行Apache的CXFWebService服务注册和调用完整示例

三、相关下载

订单ws客户端调用依赖接口包.rar,云盘下载