第一次写webservice;很是头痛。特此记录:
首先,可以用命令的方式解析wsdl文件,需要的jar包,axis.jar里的lib都有;命令行:
也可以通过eclipse来导入wsdl文件,用webservice client来生成对应的本地文件;
这是第一种方式,
XXXServiceLocator service = new XXXServiceLocator(); XXXSyscStub http_port = service.getHttp_Port(new java.net.URL(url)); http_port.setUsername("username"); http_port.setPassword("password"); Response response = http_port.SERVICE(REQuest);
第二种方式,用wsimport -d ./build-s ./src -p com.cn xxxx.wsdl(xxx?wsdl)生成java代码
从别的地方考的一个XXXSyncPortProxy.java文件,里面使一些现有的封装方法,方便编写调用service方法,如果没有也无所谓,得到了这几个java文件就好办了,首先,request和response输入输出分别用java文件,有个service的接口和实现类,先弄到service实现类的对象,(我见都在endpoint里设置)可以设置url地址,username,password,调用方法,入参出参,现在想想挺简单的,只是会用至于深层理解,还差十万八千里;看到下面的PortProxy就可以差不多整个程序就出来了
import javax.naming.InitialContext; import javax.naming.NamingException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.transform.Source; import javax.xml.ws.BindingProvider; import javax.xml.ws.Dispatch; import javax.xml.ws.Service; public class SI_WCS_RETURN_Out_SyncPortProxy{ protected Descriptor _descriptor; public class Descriptor { private SIWCSRETURNOutSyncService _service = null; private SIWCSRETURNOutSync _proxy = null; private Dispatch<Source> _dispatch = null; private boolean _useJNDIOnly = false; public Descriptor() { init(); } public Descriptor(URL wsdlLocation, QName serviceName) { _service = new SIWCSRETURNOutSyncService(wsdlLocation, serviceName); initCommon(); } public void init() { _service = null; _proxy = null; _dispatch = null; try { InitialContext ctx = new InitialContext(); _service = (SIWCSRETURNOutSyncService)ctx.lookup("java:comp/env/service/SI_WCS_RETURN_Out_SyncService"); } catch (NamingException e) { if ("true".equalsIgnoreCase(System.getProperty("DEBUG_PROXY"))) { System.out.println("JNDI lookup failure: javax.naming.NamingException: " + e.getMessage()); e.printStackTrace(System.out); } } if (_service == null && !_useJNDIOnly) _service = new SIWCSRETURNOutSyncService(); initCommon(); } private void initCommon() { _proxy = _service.getSIWCSRETURNOutSyncPort(); } publicSIWCSRETURNOutSync getProxy() { return _proxy; } public void useJNDIOnly(boolean useJNDIOnly) { _useJNDIOnly = useJNDIOnly; init(); } public Dispatch<Source> getDispatch() { if (_dispatch == null ) { QName portQName = new QName("urn:com.cn", "SI_WCS_RETURN_Out_SyncPort"); _dispatch = _service.createDispatch(portQName, Source.class, Service.Mode.MESSAGE); String proxyEndpointUrl = getEndpoint(); BindingProvider bp = (BindingProvider) _dispatch; String dispatchEndpointUrl = (String) bp.getRequestContext().get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY); if (!dispatchEndpointUrl.equals(proxyEndpointUrl)) bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, proxyEndpointUrl); } return _dispatch; } public String getEndpoint() { BindingProvider bp = (BindingProvider) _proxy; return (String) bp.getRequestContext().get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY); } public void setEndpoint(String endpointUrl,String username,String password) { BindingProvider bp = (BindingProvider) _proxy; bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointUrl); bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username); bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY,password); if (_dispatch != null ) { bp = (BindingProvider) _dispatch; bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointUrl); } } } public SI_WCS_RETURN_Out_SyncPortProxy() { _descriptor = new Descriptor(); } public SI_WCS_RETURN_Out_SyncPortProxy(URL wsdlLocation, QName serviceName) { _descriptor = new Descriptor(wsdlLocation, serviceName); } public Descriptor _getDescriptor() { return _descriptor; } public DTWCSRETURNResponse siWCSRETURNOutSync(DTWCSRETURNRequest mtWCSRETURNRequest) { return _getDescriptor().getProxy().siWCSRETURNOutSync(mtWCSRETURNRequest); } }
后来在网上翻看有这样的写法,看着也差不多,但这次完全是摸索尝试的。但生成方法,不知此一种,还有许多,
摘:
使用axis1.4调用webservice有两种简单的方式:
1、直接使用axis提供的API调用,适用于webservice接口的参数和返回值都是String的情况。
Java代码 try { Call call = (Call)new Service().createCall(); call.setTargetEndpointAddress("http://192.168.1.234:8080/TestAxis"); Object obj = call.invoke("auth", new String[]{"username","password"}); System.out.println(obj); } catch (Exception e) { e.printStackTrace(); }
2、使用axis提供的工具类org.apache.axis.wsdl.WSDL2Java先生成客户端stub,然后像使用本地方法一样调用远程接口。可以按如下方式使用WSDL2Java类,WSDL2Java后面可以是url,也可是是wsdl文件。
java -Djava.ext.dirs=e:/axis/lib org.apache.axis.wsdl.WSDL2Java http://192.168.1.234:8080/TestAxis.wsdl
这样在执行java的当前路径下就会生成TestAxis接口的客户端stub类。然后使用如下方式调用webservice,这种方式适合webservice接口的参数或返回值中包含自定义类的情况。
Java代码 try { URL url = new URL("http://192.168.1.234:8080/TestAxis"); TestAxisSoapBindingStub stub = new TestAxisSoapBindingStub(url,new Service()); Account acct = new Account(); acct.setName("username"); acct.setPassword("password"); boolean result = stub.checkAccount(acct); System.out.println(result); } catch (Exception e) { e.printStackTrace(); }
经过测试axis1.4既可以调用axis发布的服务,也可以调用CXF发布的服务。
转载自:http://blog.csdn.net/a1124544556/article/details/53932833