基于CXF webservice (2)

CXF webservice (1)方式属于code-first,今天我们来实现另外一种方式contract-frist(契约优先),这两个有啥区别,区别就是,wsdl由我们写代码生成,也就是说wsdl再生产之前是不知道的,这种方式适用于code-first,也就是(1)中的方式;如果wsdl已经生成,可以看到的,就采用contract-first,在CXF的bin中有个wsdl2java工具,可以根据这个wsdl文件生成我们需要的各种类,根据这些类就可以编写我们的测试方法了。

一、查看你的需要wsdl文件(我们依然采取(1)中的服务器端的wsdl)如下:

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://order.demo/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="OrderProcessImplService" targetNamespace="http://order.demo/">
  <wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://order.demo/" elementFormDefault="unqualified" targetNamespace="http://order.demo/" version="1.0">

  <xs:element name="Order" type="tns:order"/>

  <xs:element name="processOrder" type="tns:processOrder"/>

  <xs:element name="processOrderResponse" type="tns:processOrderResponse"/>

  <xs:complexType name="processOrder">
    <xs:sequence>
      <xs:element minOccurs="0" name="arg0" type="tns:order"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="order">
    <xs:sequence>
      <xs:element minOccurs="0" name="customerId" type="xs:string"/>
      <xs:element minOccurs="0" name="itemId" type="xs:string"/>
      <xs:element name="price" type="xs:double"/>
      <xs:element name="qty" type="xs:int"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="processOrderResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="return" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>
  </wsdl:types>
  <wsdl:message name="processOrderResponse">
    <wsdl:part element="tns:processOrderResponse" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="processOrder">
    <wsdl:part element="tns:processOrder" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="OrderProcess">
    <wsdl:operation name="processOrder">
      <wsdl:input message="tns:processOrder" name="processOrder">
    </wsdl:input>
      <wsdl:output message="tns:processOrderResponse" name="processOrderResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="OrderProcessImplServiceSoapBinding" type="tns:OrderProcess">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="processOrder">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="processOrder">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="processOrderResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="OrderProcessImplService">
    <wsdl:port binding="tns:OrderProcessImplServiceSoapBinding" name="OrderProcessImplPort">
      <soap:address location="http://localhost:8080/webService2/orderprocess"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

二、通过wsdl2java工具生成各种如下图:

基于CXF webservice (2)_第1张图片基于CXF webservice (2)_第2张图片

myTest.java

package test;


import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import demo.order.Order;
import demo.order.OrderProcess;
import demo.order.OrderProcessImplService;

public class myTest {

	@Before
	public void setUp() throws Exception {
		System.out.println("开始测试……");
	}

	@After
	public void tearDown() throws Exception {
		System.out.println("测试完成……");
	}

	@Test
	public void test() {
		Order od = new Order();
		od.setCustomerId("111");
		od.setItemId("222");
		od.setPrice(3);
		od.setQty(4);
		OrderProcessImplService factory = new OrderProcessImplService();
		OrderProcess op = factory.getOrderProcessImplPort();
		//System.out.println(od);
		System.out.println(op.processOrder(od));
		
	
	}

}

测试结果如下:




你可能感兴趣的:(基于CXF webservice (2))