Apache CXF和Spring整合实现WebService服务

开发步骤:(针对maven工程)

1、pom.xml 依赖jar包
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.0.0-milestone2</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.0.0-milestone2</version>
</dependency>
<!-- Jetty is needed if you're using the CXFServlet -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http-jetty</artifactId>
    <version>3.0.0-milestone2</version>
</dependency>

2、在要提供外部接口的类上添加@WebService注解。但有的时候,我们不希望类下面的所有方法都作为外部接口,所以在不需要向外部提供的接口的方法上添加@WebMethod(exclude=true)注解。

例:

@WebService
public class ExportServiceImpl {
    private ExportDao exportDao;

    @WebMethod(exclude=true) //表示find方法不作为WebService对外提供的接口
    public List<Export> find(Map param){
        return exportDao.find(param);
    }

    public Export get(String id){ //get方法作为WebService对外提供的接口
        return exportDao.get(id);
    }
}

3、配置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:jaxrs="http://cxf.apache.org/jaxrs"
		xmlns:jaxws="http://cxf.apache.org/jaxws"
		xmlns:cxf="http://cxf.apache.org/core"
		xsi:schemaLocation=" http://www.springframework.org/schema/beans 
							http://www.springframework.org/schema/beans/spring-beans.xsd 
							http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd 
							http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 
							http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>						
	
    <!-- 声明WebService Bean,发布WebService -->
    <bean id="exportService" class="cn.itcast.jk.service.impl.ExportServiceImpl">
    	<property name="exportDao">
    		<ref bean="exportDaoImpl"/>
    	</property>
    </bean> 
    <!-- implementor:代表要对外发布的WebService Bean; address:代表WebService访问路径 -->
    <jaxws:endpoint implementor="#exportService" address="/ExportServiceImpl"/>
    
</beans>

4、配置web.xml

<!-- 配置CXF webserice -->
<servlet>
	<servlet-name>cxf</servlet-name>
	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	<init-param>
		<param-name>config-location</param-name>
		<param-value>classpath:cxf-servlet.xml</param-value>
	</init-param>
</servlet>
<servlet-mapping>
	<servlet-name>cxf</servlet-name>
	<url-pattern>/cxf/*</url-pattern>
</servlet-mapping>

以上差不多就将cxf和spring整合了,下来启动web工程。

在浏览器地址栏输入:http://localhost:8080/xx/cxf/ExportServiceImpl?wsdl

当出现如下内容时,说明整合成功。

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.jk.itcast.cn/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="ExportServiceImplService" targetNamespace="http://impl.service.jk.itcast.cn/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://impl.service.jk.itcast.cn/" elementFormDefault="unqualified" targetNamespace="http://impl.service.jk.itcast.cn/" version="1.0">
<xs:element name="get" type="tns:get"/>
<xs:element name="getResponse" type="tns:getResponse"/>
<xs:complexType name="get">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="tns:export"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="export">
<xs:sequence>
<xs:element minOccurs="0" name="consignee" type="xs:string"/>
<xs:element minOccurs="0" name="contractIds" type="xs:string"/>
<xs:element minOccurs="0" name="createBy" type="xs:string"/>
<xs:element minOccurs="0" name="createDept" type="xs:string"/>
<xs:element minOccurs="0" name="createTime" type="xs:dateTime"/>
<xs:element minOccurs="0" name="customerContract" type="xs:string"/>
<xs:element minOccurs="0" name="destinationPort" type="xs:string"/>
<xs:element minOccurs="0" name="epnum" type="xs:string"/>
<xs:element minOccurs="0" name="extnum" type="xs:string"/>
<xs:element minOccurs="0" name="grossWeight" type="xs:double"/>
<xs:element minOccurs="0" name="id" type="xs:string"/>
<xs:element minOccurs="0" name="inputDate" type="xs:dateTime"/>
<xs:element minOccurs="0" name="lcno" type="xs:string"/>
<xs:element minOccurs="0" name="marks" type="xs:string"/>
<xs:element minOccurs="0" name="measurement" type="xs:double"/>
<xs:element minOccurs="0" name="netWeight" type="xs:double"/>
<xs:element minOccurs="0" name="priceCondition" type="xs:string"/>
<xs:element minOccurs="0" name="remark" type="xs:string"/>
<xs:element minOccurs="0" name="shipmentPort" type="xs:string"/>
<xs:element minOccurs="0" name="state" type="xs:int"/>
<xs:element minOccurs="0" name="transportMode" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="get">
<wsdl:part element="tns:get" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="getResponse">
<wsdl:part element="tns:getResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="ExportServiceImpl">
<wsdl:operation name="get">
<wsdl:input message="tns:get" name="get"></wsdl:input>
<wsdl:output message="tns:getResponse" name="getResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ExportServiceImplServiceSoapBinding" type="tns:ExportServiceImpl">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="get">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="get">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ExportServiceImplService">
<wsdl:port binding="tns:ExportServiceImplServiceSoapBinding" name="ExportServiceImplPort">
<soap:address location="http://localhost:8080/jk/cxf/ExportServiceImpl"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值