使用 WSDL4J 创建 WSDL

使用 WSDL4J 创建 WSDL

 

WSDL4J :  WSDL For JAVA Toolkit,

顾名思义,是一个解析和创建WSDL的 JAVA类库(工具包)。

 

WSDL4J来解析WSDL的例子比较多,这里不再赘述,

但用WSDL4J来创建WSDL的例子却很少,前几天根本没找到这方面的例子,

好不容易找到一个 还是在 JSR 110(Java APIs for WSDL) 的官方文档上看到的,

看来使用WSDL4J来创建WSDL 的情况比较少,

在开发 Web Service 框架 的时候可能会用到。

 

使用 WSDL4J 来创建 WSDL 并没有想象中的那么美!!!

下面是一个示例,在本人认为 容易出错 或 容易困惑 的地方做了一些注释。

 

public class CreateWsdl {
	public static void main(String[] args) throws WSDLException{
		
		String tns = "http://test.org/Hello";
		String xsd = "http://www.w3.org/2001/XMLSchema";
		
		WSDLFactory wsdlFactory = WSDLFactory.newInstance();
		// 创建一个 WSDL definition
		Definition definition = wsdlFactory.newDefinition();
			// 设置 targetNamespace
			definition.setTargetNamespace(tns);
			definition.setQName(new QName(tns,"Hello"));
			// 添加命名空间 (一些常用的命名空间)
			definition.addNamespace("tns", tns);
			definition.addNamespace("xsd", xsd);
			definition.addNamespace("wsdlsoap", "http://schemas.xmlsoap.org/wsdl/soap/");
			definition.addNamespace("soapenc11", "http://schemas.xmlsoap.org/soap/encoding/");
			definition.addNamespace("soapenc12", "http://www.w3.org/2003/05/soap-encoding");
			definition.addNamespace("soap11", "http://schemas.xmlsoap.org/soap/envelope/");
			definition.addNamespace("soap12", "http://www.w3.org/2003/05/soap-envelope");
			
			//创建 Part
			Part part1 = definition.createPart();
				part1.setName("part");
				//设置 part1 的 Schema Type 为 xsd:string
				part1.setTypeName(new QName(xsd,"string"));
			Part part2 = definition.createPart();
				part2.setName("part");
				//设置 part2 的 Schema Type 为 xsd:string
				part2.setTypeName(new QName(xsd,"string"));
				
			//创建消息(Message)
			Message message1 = definition.createMessage();
				message1.setQName(new QName(tns,"helloRequest"));
				//为 message 添加 Part
				message1.addPart(part1);
				message1.setUndefined(false);
			Message message2 = definition.createMessage();
				message2.setQName(new QName(tns,"helloResponse"));
				message2.addPart(part2);
				message2.setUndefined(false);
			definition.addMessage(message1);
			definition.addMessage(message2);
				
			//创建 portType
			PortType portType = definition.createPortType();
				portType.setQName(new QName(tns,"helloPortType"));
				//创建 Operation
				Operation operation = definition.createOperation();
					operation.setName("hello");
					//创建 Input,并设置 Input 的 message
					Input input = definition.createInput();
						input.setName("helloRequest");
						input.setMessage(message1);
					//创建 Output,并设置 Output 的 message
					Output output = definition.createOutput();
						output.setName("helloResponse");
						output.setMessage(message2);
					//设置 Operation 的输入,输出,操作类型
					operation.setInput(input);
					operation.setOutput(output);
					operation.setStyle(OperationType.REQUEST_RESPONSE);
					//这行语句很重要 !
					operation.setUndefined(false);
				portType.addOperation(operation);
				portType.setUndefined(false);
			definition.addPortType(portType);
			
			//创建绑定(binding)
			Binding binding = definition.createBinding();
				binding.setQName(new QName(tns,"helloHttpBinding"));
				//创建SOAP绑定(SOAP binding)
				SOAPBinding soapBinding = new SOAPBindingImpl();
					//设置 style = "document"
					soapBinding.setStyle("document");
					//设置 SOAP传输协议 为 HTTP
					soapBinding.setTransportURI("http://schemas.xmlsoap.org/soap/http");
				//soapBinding 是 WSDL 中的扩展元素,
				//为 binding 添加扩展元素 soapBinding 
				binding.addExtensibilityElement(soapBinding);
				//设置绑定的端口类型
				binding.setPortType(portType);
					//创建绑定操作(Binding Operation)
					BindingOperation bindingOperation = definition.createBindingOperation();
						//创建 bindingInput
						BindingInput bindingInput = definition.createBindingInput();
							bindingInput.setName("helloRequest");
								//创建 SOAP body ,设置 use = "literal"
								SOAPBody soapBody1 = new SOAPBodyImpl();
								soapBody1.setUse("literal");
							bindingInput.addExtensibilityElement(soapBody1);
						BindingOutput bindingOutput = definition.createBindingOutput();
							bindingOutput.setName("helloResponse");
								SOAPBody soapBody2 = new SOAPBodyImpl();
								soapBody2.setUse("literal");
							bindingOutput.addExtensibilityElement(soapBody2);
					//设置 bindingOperation 的名称,绑定输入 和 绑定输出
					bindingOperation.setName("hello");	
					bindingOperation.setBindingInput(bindingInput);
					bindingOperation.setBindingOutput(bindingOutput);	
				binding.addBindingOperation(bindingOperation);
				//这行语句很重要 !
				binding.setUndefined(false);
			definition.addBinding(binding);	
			
			//创建 service
			Service service = definition.createService();
				service.setQName(new QName(tns,"helloService"));
				//创建服务端口 port
				Port port = definition.createPort();
					//设置服务端口的 binding,名称,并添加SOAP地址
					port.setBinding(binding);
					port.setName("helloHttpPort");
					SOAPAddress soapAddress = new SOAPAddressImpl();
						soapAddress.setLocationURI("http://test.org/services/hello");
					port.addExtensibilityElement(soapAddress);
				service.addPort(port);
			definition.addService(service);
		
		//打印刚创建的 WSDL
		WSDLWriter wirter = wsdlFactory.newWSDLWriter();
		wirter.writeWSDL(definition, System.out);
	}
}
 

输出如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="Hello" targetNamespace="http://test.org/Hello" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://test.org/Hello" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <wsdl:message name="helloResponse">
    <wsdl:part name="part" type="xsd:string">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="helloRequest">
    <wsdl:part name="part" type="xsd:string">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="helloPortType">
    <wsdl:operation name="hello">
      <wsdl:input name="helloRequest" message="tns:helloRequest">
    </wsdl:input>
      <wsdl:output name="helloResponse" message="tns:helloResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="helloHttpBinding" type="tns:helloPortType">
    <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="hello">
      <wsdl:input name="helloRequest">
        <wsdlsoap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="helloResponse">
        <wsdlsoap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="helloService">
    <wsdl:port name="helloHttpPort" binding="tns:helloHttpBinding">
      <wsdlsoap:address location="http://test.org/services/hello"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值