JAX RCP supports a low level call API, which can be used to invoke web services is a more dynamic fashion than when using typed stubs. Although this API provides more flexibility, it is also easier to make programming mistakes. This short example illustrates how to use theCall
interface.We will use the auto loan WSDL (for more information, please see the autoloan example). The
Call
object is retrieved from the generated service. As can be seen from the client below, the call is constructed by adding the parameters and return values. Once the call has been initialized, it can be invoked.package call; import javax.xml.namespace.QName; import javax.xml.rpc.Call; import javax.xml.rpc.Service; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceFactory; import com.sssw.jbroker.web.encoding.DefaultTypeMappingRegistry; public class Client { private final static String URI = "http://circle24.com/webservices/"; public static void main(String[] args) throws Exception { | // must set the javax.xml.rpc.ServiceFactory property to | // com.sssw.jbroker.web.xml.rpc.ServiceFactoryDelegate, e.g.: | System.setProperty(ServiceFactory.SERVICEFACTORY_PROPERTY, | "com.sssw.jbroker.web.xml.rpc.ServiceFactoryDelegate"); | | // create a Novell exteNd WSSDK service factory | ServiceFactory factory = ServiceFactory.newInstance(); | | // lookup the service for Autoloan | QName qname = new QName(URI, "Autoloan"); | | // the Autoloan service does not exist | // (no stub, skeleton, or Service was generated by wsdl2java) | // but createService will return a Service object | // that can be used to create the dynamic call | Service svc = (Service) factory.createService(qname); | | QName portType = new QName(URI, "AutoloanSoap"); | Call call = svc.createCall(portType); | | QName input = new QName(URI, "Calculate"); | QName output = new QName(URI, "CalculateResponse"); | | call.addParameter(input.toString(), input, Calculate.class, ParameterMode.IN); | // Should not be declared as a parameter if it is the return type // call.addParameter(output.toString(), output, CalculateResponse.class, ParameterMode.OUT); | call.setReturnType(output, CalculateResponse.class); | | Calculate calc = new Calculate(); | calc.setMonths(24); | calc.setRateOfInterest(8); | calc.setAmount(15000); | | Object[] params = { calc }; | | call.setTargetEndpointAddress(args.length > 0 ? args[0] : | "http://upload.eraserver.net/circle24/autoloan.asmx"); | | call.setProperty(Call.SOAPACTION_URI_PROPERTY, | "http://circle24.com/webservices/Calculate"); | call.setProperty(Call.SOAPACTION_USE_PROPERTY, Boolean.TRUE); | call.setProperty(Call.OPERATION_STYLE_PROPERTY, "document"); | call.setProperty(Call.ENCODINGSTYLE_URI_PROPERTY, null); | | call.setPortTypeName(portType); | call.setOperationName(input); | | // set the type mappings | DefaultTypeMappingRegistry _mapper = (DefaultTypeMappingRegistry) | svc.getTypeMappingRegistry(); | _mapper.importTypeMappings("autoloan.wsdl.xmlrpc.type.mappings"); | | CalculateResponse res = (CalculateResponse) call.invoke(params); | String result = res.getCalculateResult(); | System.out.println(result); } }
Copyright © 2000-2003, Novell, Inc. All rights reserved.