JAX-RPC学习笔记(3)-DII方式访问webservice
接上一篇,上一篇中webservice的访问已经简化多了,但是这种方式还要依赖于一个服务器端的interface接口,可不可以不依赖任何service端的接口?这个可以有!
DII:Dynamic Invocation Interface-动态调用接口
简单的理解上一节里需要静态依赖的那个Hello接口现在可以由JAX-RPC在运行时动态生成
package com.crazycoder2010.jaxrpc; import java.net.MalformedURLException; import java.rmi.RemoteException; import javax.xml.namespace.QName; import javax.xml.rpc.Call; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.Service; import javax.xml.rpc.ServiceException; import javax.xml.rpc.ServiceFactory; public class HelloClient3 { //定义webservice的访问url private static String END_POINT = "http://localhost:8080/hello/helloKevin"; //服务名字 private static String SERVICE_NAME = "MyHello"; //port private static String PORT_NAME = "HelloPort"; //name space private static String BODY_NAMESPACE_URL = "http://www.crazycoder2010.com/wsdl/MyHello"; private static String NS_XSD = "http://www.w3.org/2001/XMLSchema"; private static String URI_ENCODING = "http://schemas.xmlsoap.org/soap/encoding/"; public static void main(String[] args)throws MalformedURLException, ServiceException, RemoteException { ServiceFactory factory = ServiceFactory.newInstance(); Service service = factory.createService(new QName(SERVICE_NAME)); QName port = new QName(PORT_NAME); Call call = service.createCall(port); call.setTargetEndpointAddress(END_POINT); call.setProperty(Call.ENCODINGSTYLE_URI_PROPERTY, URI_ENCODING); QName stringType = new QName(NS_XSD,"string");//设置返回值类型为string call.setReturnType(stringType); call.setOperationName(new QName(BODY_NAMESPACE_URL,"sayHello")); call.addParameter("String_1", stringType, ParameterMode.IN); String[] params = new String[]{"Kevin4"}; String result = (String)call.invoke(params); System.out.println(result); } }小结:
不依赖webservice服务器端的任何接口,对于减少系统耦合是个福音,不过操作过程还是繁琐了一些,有没有其他的简化方案?继续学习中。。