`
liugang594
  • 浏览: 987533 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

[CXF] Server与Client实现方式三:Provider-Dispatch

 
阅读更多

【参考: http://cxf.apache.org/docs/jax-ws-dispatch-api.html

【参考: http://cxf.apache.org/docs/provider-services.html

除了第一节介绍的jax-ws的方式和第二节介绍的simple的方式,CXF也支持Provide-Dispatch的请求-服务的方式。

 

和前两节每个operation有一个对应的method响应的方式不同,P-D模式是把整个请求信息做为处理对象来响应的,提供服务的类需要添加@WebServiceProvider注释,并实现javax.xml.ws.Provider<T>接口,支持MESSAGEPAYLOAD两种服务模式,T类型有以下选项:

  • javax.xml.transform.Source: DOMSource or SAXSource or StreamSource
  • javax.xml.soap.SOAPMessage: SOAP + MESSAGE
  • javax.activation.DataSource : HTTP + MESSAGE

 用P-D模式,就不需要有Service接口定义了。

 

一、Provider的实现

@WebServiceProvider(wsdlLocation = "DemoProvider.wsdl") //specify wsdl path
@ServiceMode(value = Mode.MESSAGE) //specify service mode
public class OrderProcessDomProvider implements Provider<DOMSource> {

	@Resource
	WebServiceContext context; //inject context
	
	public DOMSource invoke(DOMSource request) {
		try {
			
			System.out.println(context.getMessageContext().get(Message.WSDL_OPERATION)); //get request operation
			
			//get request
			MessageFactory factory = MessageFactory.newInstance();
			SOAPMessage message = factory.createMessage();
			message.getSOAPPart().setContent(request);
			message.writeTo(System.out);
			
			//construct response
			SOAPMessage response = factory.createMessage();
			SOAPElement bodyElement = response.getSOAPBody().addChildElement(new QName("<namespace>", "<responseElement>", "<localpart>"));
			SOAPElement outElement = bodyElement.addChildElement("<reponseChildElement");
			outElement.setTextContent("output");
			
			DOMSource domSource = new DOMSource();
			domSource.setNode(response.getSOAPPart());
			return domSource;
			
		} catch (SOAPException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}

上示代码中,注入了WebServiceContext用于访问与请求相关的各种信息,例如operation name,然后打印了请求信息,接下来根据wsdl中response schema的信息构造返回结果。注意这里的DomSource即是请求信息格式,也是返回信息格式。

 

二、Dispatch端

和Provider端类,在Dispatch端需要根据request schema的结构构造请求信息内容,然后使用Dispatch将信息发送:

		URL wsdlURL = new URL("DemoProvider.wsdl");
		Service service = Service.create(wsdlURL, new QName(
				"<namespace>", "<serviceName>"));
		Dispatch<DOMSource> dispatch = service.createDispatch(new QName(
				"<namespace>", "<portName>"),
				DOMSource.class, Service.Mode.MESSAGE);
		//set endpoint address
		dispatch.getRequestContext().put(Dispatch.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:8090/orderProcessProvider");

		//create request message
		MessageFactory factory = MessageFactory.newInstance();
		SOAPMessage soapMessage = factory.createMessage();
		SOAPBody soapBody = soapMessage.getSOAPBody();
		SOAPElement requestElement = soapBody.addChildElement(new QName(
				"<namespace>",
				"<requestElement>", "<localpart>"));
		SOAPElement inElement = requestElement.addChildElement(new QName("<child>"));
		inElement.setTextContent("Input");

		DOMSource source = new DOMSource();
		source.setNode(soapMessage.getSOAPPart());
		
		//invoke
		DOMSource result = dispatch.invoke(source);
		
		//show result
		SOAPMessage responseMsg = factory.createMessage();
		responseMsg.getSOAPPart().setContent(result);
		responseMsg.writeTo(System.out);

 

三、启动服务

		Endpoint.publish("http://localhost:8090/orderProcessProvider",
				new OrderProcessDomProvider());

 

分享到:
评论

相关推荐

    cxf-rt-rs-client-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-rs-client-3.0.1.jar; 赠送原API文档:cxf-rt-rs-client-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-rs-client-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-rs-client-3.0.1.pom; 包含...

    cxf-rt-frontend-simple-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-frontend-simple-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-simple-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-simple-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-...

    CXF契约优先开发方式之客户端实现(client)

    在我们的实例中,"CXF契约优先开发方式之客户端实现(client)"是基于CXF的客户端实现,它利用maven作为构建工具。Maven具有强大的插件系统,其中就包括CXF的插件,能够自动根据WSDL生成客户端的代理类。以下是如何...

    cxf-rt-frontend-jaxws-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-frontend-jaxws-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-jaxws-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-jaxws-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-jaxws...

    cxf-rt-transports-http-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-transports-http-3.0.1.jar; 赠送原API文档:cxf-rt-transports-http-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-transports-http-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-transports-...

    cxf-rt-bindings-soap-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-bindings-soap-3.0.1.jar; 赠送原API文档:cxf-rt-bindings-soap-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-bindings-soap-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-bindings-soap-...

    cxf-core-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-core-3.0.1.jar; 赠送原API文档:cxf-core-3.0.1-javadoc.jar; 赠送源代码:cxf-core-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-core-3.0.1.pom; 包含翻译后的API文档:cxf-core-3.0.1-...

    cxf-rt-databinding-jaxb-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-databinding-jaxb-3.0.1.jar; 赠送原API文档:cxf-rt-databinding-jaxb-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-databinding-jaxb-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-...

    cxf-rt-bindings-xml-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-bindings-xml-3.0.1.jar; 赠送原API文档:cxf-rt-bindings-xml-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-bindings-xml-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-bindings-xml-3.0.1....

    cxf-rt-ws-addr-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-ws-addr-3.0.1.jar; 赠送原API文档:cxf-rt-ws-addr-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-ws-addr-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-ws-addr-3.0.1.pom; 包含翻译后的API...

    cxf-rt-ws-policy-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-ws-policy-3.0.1.jar; 赠送原API文档:cxf-rt-ws-policy-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-ws-policy-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-ws-policy-3.0.1.pom; 包含...

    cxf-rt-rs-extension-providers-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-rs-extension-providers-3.0.1.jar; 赠送原API文档:cxf-rt-rs-extension-providers-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-rs-extension-providers-3.0.1-sources.jar; 赠送Maven依赖信息...

    cxf-rt-frontend-jaxrs-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-frontend-jaxrs-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-jaxrs-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-jaxrs-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-jaxrs...

    cxf-rt-wsdl-3.0.1-API文档-中文版.zip

    赠送jar包:cxf-rt-wsdl-3.0.1.jar; 赠送原API文档:cxf-rt-wsdl-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-wsdl-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-wsdl-3.0.1.pom; 包含翻译后的API文档:cxf-rt...

    CXF-JAXB包的处理问题

    如果问题仍然存在,可能需要进一步检查是否有其他第三方库也在类路径中包含了不同版本的JAXB,或者考虑升级或降级CXF和JAXB的版本以匹配兼容的组合。 总的来说,处理CXF与JAXB的版本冲突需要对Java的类加载机制有...

    cxf.xml,cxf-servlet.xml,cxf-extension-soap.xml

    &lt;import resource="classpath:META-INF/cxf/cxf.xml"/&gt; &lt;import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/&gt; &lt;import resource="classpath:META-INF/cxf/cxf-servlet.xml"/&gt;

    最新版CXF的jar包

    - `cxf-rt-frontend-jaxws.jar`:用于JAX-WS前端服务实现。 - `cxf-rt-transports-http.jar`:HTTP传输支持。 - `cxf-rt-transports-http-jetty.jar`:如果需要在Jetty上运行,需要此jar包。 - `cxf-rt-ws-security....

    Web 服务规范_第 4 部分:WS-Security源码

    在实际开发中,WS-Security的实现通常依赖于库,如Apache CXF、Axis2或Spring-WS,它们提供了API来简化WS-Security的集成。开发者可以通过这些库轻松地在SOAP消息中添加和验证安全元素。 为了实现WS-Security,...

    cxf spring server client demo.rar

    【标题】"CXF Spring Server Client Demo"是一个基于Apache CXF和Spring框架的SOAP Web服务示例项目。这个压缩包包含了一个服务器端(cxfserver)和一个客户端(cxfclient),用于展示如何使用CXF和Spring来创建、...

    CXF 2.4 WebService 发布和调用的身份验证和获取示例代码

    配置: trial-cxf-server.xml 其中配置了对传入请求的拦截器用以验证调用者身份 验证程序: WsServerAuthHandler. 这里只需要提供调用者应该使用的正确的口令. 是否和调用者实际传入的口令一致,由cxf完成. web.xml ...

Global site tag (gtag.js) - Google Analytics