webservice-cxf-与spring整合

来源:互联网 发布:淘宝开店攻略创业篇 编辑:程序博客网 时间:2024/06/05 13:35

server

Maven

<!-- CXF -->        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-frontend-jaxws</artifactId>            <version>2.4.3</version>        </dependency>        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-transports-http</artifactId>            <version>2.4.3</version>            <exclusions>                <exclusion>                    <groupId>org.springframework</groupId>                    <artifactId>spring-web</artifactId>                </exclusion>            </exclusions>        </dependency>        <!-- Jetty is needed if you're are not using the CXFServlet -->        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-transports-http-jetty</artifactId>            <version>2.4.3</version>        </dependency>

web.xml

<context-param>        <param-name>contextConfigLocation</param-name>        <param-value>            /WEB-INF/config/applicationContext-base.xml;            /WEB-INF/config/applicationContext-ws.xml;        </param-value></context-param>

applicationContext-ws.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-3.0.xsd            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">    <description>web service 配置,在实际应用中服务端和客户端位于不同的服务环节中,因此本配置文件也要分开部署.    </description><!-- Start 服务端配置 -->  <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"/> <bean id="BillService" class="com.aiso.basic.ws.ds.server.YstBillServiceImpl" />  <jaxws:endpoint id="billService" implementor="#BillService" address="/BillService"/>   <bean id="ReceiptService" class="com.aiso.basic.ws.ds.server.YstReceiptServiceImpl" />  <jaxws:endpoint id="receiptService" implementor="#ReceiptService" address="/ReceiptService"/></beans>

接口

import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;@WebServicepublic interface YstReceiptService {    @WebMethod    public @WebResult(name="result")int putOperateStorage(@WebParam(name="osContent")String osContent,  @WebParam(name="sign")String sign);}

实现类

public class YstReceiptServiceImpl implements YstReceiptService {    @Override    public int putOperateStorage(String osContent, String sign) {        log.debug("params:osContent=" + osContent  + ",sign=" + sign);        System.err.println("服务端:========================================");        System.err.println("osContent:" + osContent);        System.err.println("-----------------------------------------------");        System.err.println(" sign:" + sign);        System.err.println("---------------------------------------------");        int result = 0;        // 验证密钥        if (!Md5Util.MD5(osContent).equals(sign.toLowerCase())) {            System.err.println("密钥不正确!");            result = 1;            return result;        }        // 将osContent解析为数据库数据        if(!osContent.startsWith("[")){            osContent ="["+ osContent +"]";            System.err.println(osContent);        }        JSONArray jsArray = JSONArray.fromObject(osContent);        Object[] inStorageStrs = jsArray.toArray();        ...}

client

调用类

@Repository("clientCallDao")public class ClientCallDao extends EntityDaoImpl {    Log log = LogFactory.getLog(super.getClass());    public int clientTestBill() {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                new String[] { "com/aiso/basic/ws/ds/client/bean/client-beans.xml" });        YstBillService client = (YstBillService) context.getBean("billService2");}

client-beans.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    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">    <jaxws:client id="billService2" serviceClass="com.aiso.basic.ws.ds.client.bean.ystbill.YstBillService"        address="http://localhost:9090/aiso/services/BillService?wsdl" />    <jaxws:client id="receiptService2" serviceClass="com.aiso.basic.ws.ds.client.bean.ystreceipt.YstReceiptService"        address="http://localhost:9090/aiso/services/ReceiptService?wsdl" /></beans>

生成所需类的命令

1.下载配置apache-cxf-2.4.3
2.配置用户环境变量 如Path:F:\apache-cxf-2.4.3\bin;
3.进入命令行 输入以下命令:
wsdl2java -p com.aiso.basic.ws.ds.client.bean.ystbill -frontend jaxws21 -d d:\webservice -keep -verbose http://localhost:9090/aiso/services/BillService?wsdl
4.将生成的client代码 拷贝到项目中
5.添加主方法 运行 如果是web项目 写好配置文件 搭建好运行环境
6.运行!

wsdl2java -p com.aiso.basic.ws.ds.client.bean.ystreceipt -frontend jaxws21 -d d:\webservice -keep -verbose http://localhost:9999/aiso/services/ReceiptService?wsdl

jdk 自带工具
D:/test/test>wsimport -keep http://123.116.125.201:9090/aiso/services/BillService?wsdl

生成的类

/** * This object contains factory methods for each  * Java content interface and Java element interface  * generated in the com.mzdp.basic.ws.ds.client.bean.ystbill package.  * <p>An ObjectFactory allows you to programatically  * construct new instances of the Java representation  * for XML content. The Java representation of XML  * content can consist of schema derived interfaces  * and classes representing the binding of schema  * type definitions, element declarations and model  * groups.  Factory methods for each of these are  * provided in this class. *  */@XmlRegistrypublic class ObjectFactory {    private final static QName _PutBill_QNAME = new QName("http://server.ds.ws.basic.aiso.com/", "putBill");    private final static QName _PutBillResponse_QNAME = new QName("http://server.ds.ws.basic.aiso.com/", "putBillResponse");    /**     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.mzdp.basic.ws.ds.client.bean.ystbill     *      */    public ObjectFactory() {    }    /**     * Create an instance of {@link PutBill }     *      */    public PutBill createPutBill() {        return new PutBill();    }    /**     * Create an instance of {@link PutBillResponse }     *      */    public PutBillResponse createPutBillResponse() {        return new PutBillResponse();    }    /**     * Create an instance of {@link JAXBElement }{@code <}{@link PutBill }{@code >}}     *      */    @XmlElementDecl(namespace = "http://server.ds.ws.basic.aiso.com/", name = "putBill")    public JAXBElement<PutBill> createPutBill(PutBill value) {        return new JAXBElement<PutBill>(_PutBill_QNAME, PutBill.class, null, value);    }    /**     * Create an instance of {@link JAXBElement }{@code <}{@link PutBillResponse }{@code >}}     *      */    @XmlElementDecl(namespace = "http://server.ds.ws.basic.aiso.com/", name = "putBillResponse")    public JAXBElement<PutBillResponse> createPutBillResponse(PutBillResponse value) {        return new JAXBElement<PutBillResponse>(_PutBillResponse_QNAME, PutBillResponse.class, null, value);    }}
/** * <p>Java class for putBill complex type. *  * <p>The following schema fragment specifies the expected content contained within this class. *  * <pre> * &lt;complexType name="putBill"> *   &lt;complexContent> *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> *       &lt;sequence> *         &lt;element name="billContent" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/> *         &lt;element name="sign" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/> *       &lt;/sequence> *     &lt;/restriction> *   &lt;/complexContent> * &lt;/complexType> * </pre> *  *  */@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "putBill", propOrder = {    "billContent",    "sign"})public class PutBill {    protected String billContent;    protected String sign;    /**     * Gets the value of the billContent property.     *      * @return     *     possible object is     *     {@link String }     *          */    public String getBillContent() {        return billContent;    }    /**     * Sets the value of the billContent property.     *      * @param value     *     allowed object is     *     {@link String }     *          */    public void setBillContent(String value) {        this.billContent = value;    }    /**     * Gets the value of the sign property.     *      * @return     *     possible object is     *     {@link String }     *          */    public String getSign() {        return sign;    }    /**     * Sets the value of the sign property.     *      * @param value     *     allowed object is     *     {@link String }     *          */    public void setSign(String value) {        this.sign = value;    }}
/** * <p>Java class for putBillResponse complex type. *  * <p>The following schema fragment specifies the expected content contained within this class. *  * <pre> * &lt;complexType name="putBillResponse"> *   &lt;complexContent> *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> *       &lt;sequence> *         &lt;element name="result" type="{http://www.w3.org/2001/XMLSchema}int"/> *       &lt;/sequence> *     &lt;/restriction> *   &lt;/complexContent> * &lt;/complexType> * </pre> *  *  */@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "putBillResponse", propOrder = {    "result"})public class PutBillResponse {    protected int result;    /**     * Gets the value of the result property.     *      */    public int getResult() {        return result;    }    /**     * Sets the value of the result property.     *      */    public void setResult(int value) {        this.result = value;    }}
/** * This class was generated by Apache CXF 2.4.3 * 2015-01-27T15:35:41.466+08:00 * Generated source version: 2.4.3 *  */@WebServiceClient(name = "YstBillServiceImplService",                   wsdlLocation = "http://localhost:9999/aiso/services/BillService?wsdl",                  targetNamespace = "http://server.ds.ws.basic.aiso.com/") public class YstBillServiceImplService extends Service {    public final static URL WSDL_LOCATION;    public final static QName SERVICE = new QName("http://server.ds.ws.basic.aiso.com/", "YstBillServiceImplService");    public final static QName YstBillServiceImplPort = new QName("http://server.ds.ws.basic.aiso.com/", "YstBillServiceImplPort");    static {        URL url = null;        try {            url = new URL("http://localhost:9999/aiso/services/BillService?wsdl");        } catch (MalformedURLException e) {            java.util.logging.Logger.getLogger(YstBillServiceImplService.class.getName())                .log(java.util.logging.Level.INFO,                      "Can not initialize the default wsdl from {0}", "http://localhost:9999/aiso/services/BillService?wsdl");        }        WSDL_LOCATION = url;    }    public YstBillServiceImplService(URL wsdlLocation) {        super(wsdlLocation, SERVICE);    }    public YstBillServiceImplService(URL wsdlLocation, QName serviceName) {        super(wsdlLocation, serviceName);    }    public YstBillServiceImplService() {        super(WSDL_LOCATION, SERVICE);    }    /**     *     * @return     *     returns YstBillService     */    @WebEndpoint(name = "YstBillServiceImplPort")    public YstBillService getYstBillServiceImplPort() {        return super.getPort(YstBillServiceImplPort, YstBillService.class);    }    /**     *      * @param features     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.     * @return     *     returns YstBillService     */    @WebEndpoint(name = "YstBillServiceImplPort")    public YstBillService getYstBillServiceImplPort(WebServiceFeature... features) {        return super.getPort(YstBillServiceImplPort, YstBillService.class, features);    }}
/** * <p>Java class for putBillResponse complex type. *  * <p>The following schema fragment specifies the expected content contained within this class. *  * <pre> * &lt;complexType name="putBillResponse"> *   &lt;complexContent> *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> *       &lt;sequence> *         &lt;element name="result" type="{http://www.w3.org/2001/XMLSchema}int"/> *       &lt;/sequence> *     &lt;/restriction> *   &lt;/complexContent> * &lt;/complexType> * </pre> *  *  */@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "putBillResponse", propOrder = {    "result"})public class PutBillResponse {    protected int result;    /**     * Gets the value of the result property.     *      */    public int getResult() {        return result;    }    /**     * Sets the value of the result property.     *      */    public void setResult(int value) {        this.result = value;    }}