一. 什么是QName
1.来历:qname是qualifiedname的简写
2.构成:由名字空间(namespace)前缀(prefix)以及冒号(:),还有一个元素名称构成
3.举例:<wsdl:definitions name="Helloworld" targetNamespace="http://server.com/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">.....<wsdl:portTypename="IHelloWorldService"><wsdl:operation name="sayHello"><wsdl:inputmessage="tns:sayHello" name="sayHello" /><wsdl:outputmessage="tns:sayHelloResponse" name="sayHelloResponse" /></wsdl:operation></wsdl:portType></wsdl:definitions>以wsdl:portType为例:wsdl是名字空间前缀,portTpye是元素名称,wsdl:portType就是一个qname,其namespace是:http://server.com/
二. QName在CXF中的使用
直接上代码,我使用的是apache-cxf-2.4.6.
服务器端(注意我测试的时候service接口,service实现类 和发布service的类放在同一包里,实际使用过程中可以放在不同的包里)
1. 服务器端代码:
1.1 service接口
- package com.server;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- @WebService
- public interface IHelloWorldService {
- public String sayHello(@WebParam(name="text") String name);
- }
- 1.2 service 实现类
- package com.server;
- import javax.jws.WebService;
- @WebService(serviceName="Helloworld")
- public class HelloWorldService implements IHelloWorldService{
- public String sayHello( String name){
- return name + "say : Hello Service.";
- }
- }
- 1.3 发布service的类
- <pre class="java" name="code">package com.server;
- import javax.xml.ws.Endpoint;
- public class DeployHelloWorldService {
- public static void main(String[] args) throws Exception{
- IHelloWorldService service = new HelloWorldService();
- String address = "http://localhost:9000/helloWorld";
- Endpoint.publish(address, service);
- System.out.println("service ready ...");
- }
- }</pre>
- 1.4 运行发布的类后,在IE中输入:<a href="http://localhost:9000/helloWorld?wsdl" mce_href="http://localhost:9000/helloWorld?wsdl">http://localhost:9000/helloWorld?wsdl</a>就可以看到如下wsdl:
- <?xml version="1.0" encoding="UTF-8" ?>
- - <wsdl:definitions name="Helloworld" targetNamespace="http://server.com/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- - <wsdl:types>
- - <xs:schema elementFormDefault="unqualified" targetNamespace="http://server.com/" version="1.0" xmlns:tns="http://server.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
- <xs:element name="sayHello" type="tns:sayHello" />
- <xs:element name="sayHelloResponse" type="tns:sayHelloResponse" />
- - <xs:complexType name="sayHello">
- - <xs:sequence>
- <xs:element minOccurs="0" name="text" type="xs:string" />
- </xs:sequence>
- </xs:complexType>
- - <xs:complexType name="sayHelloResponse">
- - <xs:sequence>
- <xs:element minOccurs="0" name="return" type="xs:string" />
- </xs:sequence>
- </xs:complexType>
- </xs:schema>
- </wsdl:types>
- - <wsdl:message name="sayHelloResponse">
- <wsdl:part element="tns:sayHelloResponse" name="parameters" />
- </wsdl:message>
- - <wsdl:message name="sayHello">
- <wsdl:part element="tns:sayHello" name="parameters" />
- </wsdl:message>
- - <wsdl:portType name="IHelloWorldService">
- - <wsdl:operation name="sayHello">
- <wsdl:input message="tns:sayHello" name="sayHello" />
- <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" />
- </wsdl:operation>
- </wsdl:portType>
- - <wsdl:binding name="HelloworldSoapBinding" type="tns:IHelloWorldService">
- <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
- - <wsdl:operation name="sayHello">
- <soap:operation soapAction="" style="document" />
- - <wsdl:input name="sayHello">
- <soap:body use="literal" />
- </wsdl:input>
- - <wsdl:output name="sayHelloResponse">
- <soap:body use="literal" />
- </wsdl:output>
- </wsdl:operation>
- </wsdl:binding>
- - <wsdl:service name="Helloworld">
- - <wsdl:port binding="tns:HelloworldSoapBinding" name="HelloWorldServicePort">
- <soap:address location="http://localhost:9000/helloWorld" />
- </wsdl:port>
- </wsdl:service>
- </wsdl:definitions>
2. 客户端使用QName调用WebService的代码
- package client;
- import javax.xml.namespace.QName;
- import javax.xml.ws.Service;
- import javax.xml.ws.soap.SOAPBinding;
- import com.server.IHelloWorldService;
- public class HelloWorldServiceClient {
- /**
- * namespaceURI即为:wsdl:definitions name="Helloworld" targetNamespace="http://server.com/"...
- * 中的targetNamespace.
- * 如果服务器端的service接口和类不在同一个包中时:
- * namespaceURI即为wsdl:import中的namespace
- * <wsdl:import location="http://localhost:9000/helloWorld?wsdl=IHelloWorldService.wsdl" namespace="http://inter.server.com/" />
- */
- private static String namespaceURI = "http://server.com/";
- private static final QName SERVICE_NAME = new QName(namespaceURI,"IHelloWorldService");
- private static final QName PORT_NAME = new QName(namespaceURI,"IHelloWorldServicePort");
- public static void main(String[] args) {
- Service service = Service.create(SERVICE_NAME);
- String endpointAddress = "http://localhost:9000/helloWorld";
- service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
- IHelloWorldService clientService = service.getPort(IHelloWorldService.class);
- String result = clientService.sayHello("hello QName");
- System.out.println(result);
- }
运行该类就可以通过QName调用到Server端的service
注意以下几点:
2.1 namespaceURI 为:wsdl:definitions中的targetNamespace的值,在本例中就是http://server.com/
2.2.serviceName 为: wdsl中的IHelloWorldService对对应的portType 的name ,使用<wsdl:service name="Helloworld">中的Helloworld也可以,具体原因也说不上来。serviceName使用其他错误的值,该程序
也可以正常运行,搞不懂了。
2.3.protName 为wdsl中的IHelloWorldService对对应的portType 的name + “Port”即IHelloWorldServicePort,我估计之所以这么使用是因为客户端是通过IHelloWorldService调用的原因。
使用HelloworldPort会抛异常:
“Caused by: java.net.MalformedURLException: Invalid address. Endpoint address cannot be null.”
这个PortName也不是<wsdl:port binding="tns:HelloworldSoapBinding" name="HelloWorldServicePort"> 中的HelloWorldServicePort,如果使用HelloWorldServicePort也回抛出上面的异常。
总结一点,在使用QName的过程中nameSpaceUrl 和PortName 不能写错,否则会导致调用时抛异常。
相关推荐
在本教程中,我们将深入探讨如何使用 CXF 2.5.9 发布和调用 Web 服务。 1. **环境准备** 在开始之前,确保你的开发环境中已经安装了 Java 开发工具(JDK)和 Maven,因为 CXF 基于 Maven 进行构建和管理依赖。同时...
), new QName("http://demo.cxf/", "HelloWorld")); HelloWorld hello = service.getPort(HelloWorld.class); System.out.println(hello.sayHello("world")); } } ``` 5. **运行客户端** 运行`CXFClient`类...
本示例代码是基于Eclipse集成开发环境的一个项目,旨在帮助开发者理解和实践如何在Java中使用CXF来实现Web服务。 首先,我们需要了解Web服务的基本概念。Web服务是一种通过互联网进行通信的应用程序接口(API)。它...
【正文】 在IT行业中,Web服务是不同系统间进行数据交换和交互的一种标准方式,而Apache CXF是一个开源框架,专门用于...在实际项目中,理解并熟练掌握CXF的使用,可以显著提高开发效率,同时降低系统间的集成难度。
URL wsdlURL = HelloWorld.class.getResource("HelloWorld.wsdl"); QName qname = new QName("http://example.com/hello", "HelloWorldImplPort"); HelloWorldImpl service = new HelloWorldImpl(); Service ss ...
注意,在此示例中使用了JDK 5引入的注解特性。如果未添加`@SOAPBinding(style = Style.RPC)`注解,则可能会遇到异常。对于JDK 1.6及以下版本,也需要特别注意这一问题。解决方案包括使用`apt`命令进行预编译或者升级...
在Android平台上使用CXF Webservice,你需要先在项目中引入CXF的依赖库。这通常通过Maven或Gradle构建系统完成,添加对应的依赖项到你的build.gradle文件中。例如,如果你使用Gradle,可以在dependencies块内添加...
), new QName("http://example.com/", "HelloWorld")); HelloWorld proxy = service.getPort(HelloWorld.class); System.out.println(proxy.sayHello("User")); ``` - REST客户端(使用HttpURLConnection): `...
HelloWorld helloWorld = service.getPort(HelloWorld.class); System.out.println(helloWorld.sayHello("World")); ``` 接下来,我们转向CXF框架,它提供了一种更简洁、更强大的方式来处理Web服务。CXF不仅支持JAX...
org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:7002/card/services/HelloWorld?wsdl"); QName name = new QName("http://dao.xcf.digitalchina.com/", "sayHello"); String xmlStr =...
HelloWorld proxy = service.getPort(HelloWorld.class); String response = proxy.sayHello("World"); System.out.println(response); // 输出 "Hello, World" ``` 以上就是使用Java JDK发布SOAP Web服务的基本...
本示例将探讨如何在Spring框架中配置最基础的Web服务,特别关注的是基于CXF的Web服务。CXF是一个强大的开源框架,它支持SOAP和RESTful风格的Web服务,使得开发者能够方便地创建和消费Web服务。 首先,我们需要理解...
call.setOperationName(new QName("http://localhost/HelloWorld", "getName")); call.setTargetEndpointAddress(endpoint); String result = (String) call.invoke(new Object[]{args[0]}); System.out.println...
Service service = Service.create(new URL("http://localhost:8080/HelloWorldService?wsdl"), QName.valueOf("http://example.com/helloworld", "HelloWorldService")); HelloWorldService port = service.getPort...
XFire是Apache CXF项目的前身,它提供了一种简单的方法来创建和使用Web服务。XFire支持多种协议,包括SOAP、REST(Representational State Transfer)、JMS(Java Message Service)等,并且与Spring框架有良好的...
使用JAX-WS(Java API for XML Web Services)或者Apache CXF等库,我们可以将这个类暴露为一个SOAP服务。例如: ```java import javax.jws.WebService; @WebService public class MyWebService { public String ...
为了测试Web服务,我们可以使用多种工具,比如JBossWS自带的客户端示例或者使用Apache CXF的`wsdl2java`工具生成客户端代码。生成的客户端代码将提供一个易于使用的接口来调用Web服务。例如,对于上面的`...