`
步行者
  • 浏览: 167599 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

使用 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>
 

 

分享到:
评论
6 楼 shenning4321 2011-12-07  
请问一下  我怎么把生成的wsdl写入一个文件中
5 楼 jkleeo 2010-03-05  
用AXIS根据WSDL生成代码,需要用到WSDL.
没有WSDL,WS就没办法工作.
4 楼 步行者 2009-08-20  
641216927 写道
请问一下,自己创建WSDL的意义何在??

在开发 Web Service 框架 的时候可能会用到
3 楼 641216927 2009-08-20  
请问一下,自己创建WSDL的意义何在??
2 楼 步行者 2009-07-08  
whatwhat 写道
曾经用wsdl4j解析过wsdl
没用它创建过,
自动创建wsdl的情况应该很少
axis,xfire,cxf 这些框架应该用到了这种功能

恩,确实很少用到
java 的一些 web service 框架 应该都用到了这种功能
1 楼 whatwhat 2009-07-08  
曾经用wsdl4j解析过wsdl
没用它创建过,
自动创建wsdl的情况应该很少
axis,xfire,cxf 这些框架应该用到了这种功能

相关推荐

    WSDL for C++ Toolkit-开源

    C++ 工具包的 Web 服务描述语言 (WSDL4CPP) 允许创建、表示和操作 WSDL 文档。 它是 WSDL4J 工具包的 C++ 端口。

    wsdl2java源码-grails-cxf-grails-3:grails-cxf-grails-3

    WSS4J 2.x 以确保安全。 请注意,在 cxf 3.0.4(插件 v 2.1+ 和 3.x)中,很多东西都被移动了。 例如,我们必须添加 org.apache.cxf.xjc.runtime.DataTypeAdapter 来代替之前包含的项目。 圣杯 CXF 插件 介绍 Grails...

    WebService所用到的jar包

    axis.jar,castor-xml.jar,commons-discovery.jar,commons-logging.jar,jaxrpc.jar,jdom.jar,log4j.jar,saaj.jar,wsdl4j.jar包,使用这些包可以直接创建WebSservice接口的测试。

    xfire-all-1.2.6.jar

    使用xfire创建webservice是需要用的所有包,还有跟spring集成时(如果缺少)可能会报错的包,希望对大家有用 xfire-all-1.2.6.jar,jdom.jar,wsdl4j-1.5.1.jar,xbean-spring-2.8.jar

    java SOAPHEADER的web service

    c# JSON返回格式的WEB SERVICE//www.jb51.net/article/16768.htm我这里是利用axis创建类包的,首先下载axis的压缩包,然后解压,把axis放到webapp下面。...wsdl4j.jar;saaj.jar;jaxrpc.jar;mail.jar” org.apac

    AXIS开发webservice

    \axis-bin-1_4\axis-1_4\webapps\axis\WEB-INF\lib\*.jar 注意:这些包在我们下载下来的AXIS:axis-bin-1_4.zip中 3. 修改WEB.XML文件,最简单的方法是:直接把 \axis-bin-1_4\axis-1_4\webapps\axis\WEB-INF\web....

    axis1.4 部署解析webservie

    D:\axis\WEB-INF\lib\wsdl4j-1.5.1.jar; D:\axis\WEB-INF\lib\xmlsec.jar 启动TOMCAT,浏览器输入http://localhost:8080/axis/HelloWorldService.jws?wsdl 会出现wsdl页面。说明可以提供正常服务了。 现在可以制作...

    Spring in Action(第2版)中文版

    a.4spring与log4j 附录b用(和不用)spring进行测试 b.1测试简介 b.1.1理解不同类型的测试 b.1.2使用junit b.1.3spring在测试中的角色 b.2单元测试springmvc控制器 b.2.1模拟对象 b.2.2断言modelandview的内容...

    Spring in Action(第二版 中文高清版).part2

    A.4 Spring与Log4j 附录B 用(和不用)Spring进行测试 B.1 测试简介 B.1.1 理解不同类型的测试 B.1.2 使用JUnit B.1.3 Spring在测试中的角色 B.2 单元测试Spring MVC控制器 B.2.1 模拟对象 B.2.2 断言...

    Spring in Action(第二版 中文高清版).part1

    A.4 Spring与Log4j 附录B 用(和不用)Spring进行测试 B.1 测试简介 B.1.1 理解不同类型的测试 B.1.2 使用JUnit B.1.3 Spring在测试中的角色 B.2 单元测试Spring MVC控制器 B.2.1 模拟对象 B.2.2 断言...

    web Services服务器所需要的基础包

    包括 commons-httpclient-3.0.jar commons-logging-1.0.4.jar jdom-1.0.jar spring-1.2.6.jar wsdl4j-1.6.1.jar xbean-spring-2.8.jar xfire-all-1.2.6.jar xfire-core-1.2.6.jar

    java8stream源码-hello-world:我测试存储库

    java8流源码你好,世界 ...上是:log4j 2.3 版。或者也许使用 slf4j ??)Angular 基础...... InteliJ ! (看看你如何使用 GIT 和 MAVEN 与 INTELIJ 一起工作!!!)Jacson JAXB WSDL () JMS、JMX、JTA Message

    SOA-J-开源

    以WSDL为中心的Java框架,用于创建和部署面向服务的体系结构(SOA)的Web服务组件。

    dive into python

    目录 1. 安装 Python 1.1. 哪一种 Python 适合您? 1.2. Windows 上的 Python 1.3. Mac OS X 上的 Python 1.4. Mac OS 9 上的 Python 1.5. RedHat Linux 上的 Python ...J.B.4. CWI 许可声明与免责声明

    JAVA社区交流平台网站

    2.2.2 Dom4j 6 2.2.3 SOAP 6 2.2.4 WebService 7 2.2.5 WSDL 7 2.2.6 Velocity 8 2.2.7 Swing 8 2.2.8 Java反射 9 2.3 本章小结 9 第三章 服务开发平台的总体框架 10 3.1 前台可视化IDE的设计 11 3.2 后台的总体设计...

    net学习笔记及其他代码应用

    使用WSDL.exe命令行工具。 2.使用VS.NET中的Add Web Reference菜单选项 23..net Remoting 的工作原理是什么? 答:服务器端向客户端发送一个进程编号,一个程序域编号,以确定对象的位置。 24.在C#中,string ...

    J2EE面试题

    2:设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。 3:用冒泡法对10个数排序(由小到大)例如: 54,12,-6,6,22,-7,9,0,999,79 4:有一个登录页面,上面有用户名(name),...

    超级有影响力霸气的Java面试题大全文档

    4. 多态性:  多态性是指允许不同类的对象对同一消息作出响应。多态性包括参数化多态性和包含多态性。多态性语言具有灵活、抽象、行为共享、代码共享的优势,很好的解决了应用程序函数同名问题。 5、String是最...

    asp.net面试题

    您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性 序列化:序列化是将对象转换为容易传输的格式的过程。例如,可以序列化一个对象...

    java 面试题 总结

    28、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。 以下程序使用内部类实现线程,对j增减的时候没有考虑顺序问题。 public class ThreadTest1{ private int j; public static ...

Global site tag (gtag.js) - Google Analytics