采用CXF 构建webservice

来源:互联网 发布:边际报酬递减规律算法 编辑:程序博客网 时间:2024/05/19 23:52

一般接口开发都会使用如 webservice,ftp ,sftp 的方式实现,如java 程序访问.net 程序,就不的不涉及到soa



开发步骤:

1.下载cxf 相关jar 包

2.在spring context配置文件中引入以下cxf配置

<import resource="classpath*:META-INF/cxf/cxf.xml" /><import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" /><import resource="classpath*:META-INF/cxf/cxf-servlet.xml" />

3.在web.xml中添加过滤器:
<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>/webservices/*</url-pattern></servlet-mapping>4.定义服务端WebService接口(下面直接使用了类定义,当然也可用接口定义)  package com.jumbo.webservice.biaogan.service;import java.util.List;import javax.jws.WebParam;import javax.jws.WebService;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import com.jumbo.manager.warehouse.WareHouseManagerProxy;import com.jumbo.mq.MarshallerUtil;............................./**
* 使用@WebService将接口中的所有方法输出为Web Service.* 可用annotation对设置方法、参数和返回值在WSDL中的定义.
下面是定义了两个带参数的方法
 * Please modify this class to meet your needs This class is not complete
 */

@WebService(serviceName = "BgService", targetNamespace = "http://www.jumbomart.cn/webservice/")
public class BGWebService {
    
    protected static final Logger log = LoggerFactory.getLogger(BGService.class);

    public String outboundToBz(@WebParam(name = "request") String xml) {

    }

    public String inboundToBz(@WebParam(name = "request") String xml) {
    
    }

}


5.webservice 服务端配置

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-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
                        http://cxf.apache.org/jaxrs
                        http://cxf.apache.org/schemas/jaxrs.xsd">
    <bean id="jaxbBean" class="org.apache.cxf.jaxb.JAXBDataBinding" scope="prototype" />

    <context:component-scan base-package="com.jumbo.webservice.biaogan.service">
        <context:include-filter type="annotation"
            expression="org.aspectj.lang.annotation.Aspect" />
    </context:component-scan>

    <bean id="bgService" class="com.webservice.service.BGWebService " />

    <jaxws:endpoint id="bgAddress" implementor="#bgService" address="/bg" />


最后可以通过http://localhost:8080/testws/services/bg?wsdl 查看自动生成的wsdl 文件
wsdl 文档太长,略

6.可以通过eclipse webservice 插件来通过wsdl 文件自动构建客服端,从而向服务器发消息