Sie sind auf Seite 1von 19

Developing web services using JAX-RPC

Settings: a. Install Java(TM) Web Services Developer Pack 2.0, Java 5.0 b. Set JAVA_HOME, JWSDP_HOME(to C:\Sun\jwsdp-2.0). PATH environment variable should include C:\Sun\jwsdp-2.0\jwsdp-shared\bin.

Coding the Service Endpoint Interface and Implementation Class


HelloIF interface(SEI): package helloservice; import java.rmi.Remote; import java.rmi.RemoteException; public interface HelloIF extends Remote { public String sayHello(String s) throws RemoteException; } Implementation: package helloservice; public class HelloImpl implements HelloIF { public String message ="Hello"; public String sayHello(String s) { return message + s; }

Compile the above files. Building the Service Use wscompile to generate the .wsdl and mapping.xml files. Before generating the files, write a configuration file [config-interface.xml] which will be used to generate these.
<?xml version="1.0" encoding="UTF-8"?> <configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config"> <service name="MyHelloService" targetNamespace="urn:Foo" typeNamespace="urn:Foo" packageName="helloservice"> <interface name="helloservice.HelloIF"/> </service> </configuration>

Author: Ravindra V

Page 1

Note: targetNamespace, typeNamespace could be anything. There should a directory with the name build which will save generated .wsdl file. mapping.xml will be generated in the same path where wscompile command is run. Use the below command: wscompile -define -mapping mapping.xml -d build -nd build -classpath <path where class files exists> <path to config-interface.xml> Example: wscompile -define -mapping mapping.xml -d build -nd build -classpath "C:\Eclipse-i3.1\e-i3.1\runtime-workbench-workspace\BPMServices" "C:\ravi\configinterface.xml" Packaging and Deploying the Service Use ant tool [build.xml] file to generate the .ear file. build.xml
<project name="buildWebservice" default="build-ear"> <target name="build-ear"> <taskdef name="servicegen" classname="weblogic.ant.taskdefs.webservices.servicegen.ServiceGenTask" classpath="C:\bea\weblogic81\server\lib\webservices.jar"/> <servicegen destEar="MyHelloService.ear" warName="MyHelloService.war" contextURI="web_services"> <service javaClassComponents="helloservice.HelloImpl" targetNamespace="urn:Foo" serviceName="MyHelloService" serviceURI="/MyHelloService" generateTypes="True" expandMethods="True" style="RPC"> </service> </servicegen> </target> </project>

Note: weblogic.jar file should be in the CLASSPATH. Copy mapping.xml, MyHelloService.wsdl, build.xml and class files (for SEI and its implementation - with package folder) in the same directory (say c:\ravi). Also place the path to the class files in the CLASSPATH environment variable. Deploy the .ear file as an application in weblogic. Web service can be accessed using the URL http://hostname:contextURI/serviceName
and the WSDL at http://hostname:contextURI/serviceName?WSDL

Author: Ravindra V

Page 2

Accessing a web service from a client program using JAX-RPC Static Stub Client: Uses JAX-RPC to call the web method from Stubs generated at the client end. 1. Generating the stubs a. Prepare the configuration file [config-wsdl.xml] <configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config"> <wsdl location="http://localhost:7001/web_services/MyHelloService?WSDL" packageName="staticstub"/> </configuration> b. Use the below command to generate stubs wscompile -gen:client -d build -classpath build <path to config-wsdl.xml> Stubs will be created in build directory. jar these files and place it in build path. 2. Write the client program
package client; import helloservice.*; import javax.xml.rpc.Stub; import staticstub.*; public class HelloClient { private String endpointAddress; public static void main(String[] args) { try { Stub stub = createProxy(); stub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY,"http://lo calhost:7001/web_services/MyHelloService?WSDL"); HelloIF hello = (HelloIF)stub; System.out.println(hello.sayHello("Ravi")); } catch (Exception ex) { ex.printStackTrace(); } } private static Stub createProxy() { // Note: MyHelloService_Impl is implementation-specific. return (Stub)(new MyHelloService_Impl().getMyHelloServicePort());

Author: Ravindra V

Page 3

Dynamic Proxy Client: Uses JAX-RPC to call web methods from the Service object at runtime by referring the WSDL. 1. Generate client artifacts using wsimport a. Make sure that the classpath contains all the jars in the paths C:\Sun\jwsdp-2.0\jaxb\lib and C:\Sun\jwsdp-2.0\jaxws\lib. b. Generate the build.xml file.
<project name="buildWSImport" default="build-wsimport"> <target name="build-wsimport"> <taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport"> <classpath path="jaxws.classpath"/> </taskdef> <wsimport keep="true" wsdl="D:\more_changes\04-June2008\MyHelloService.wsdl"> </wsimport> </target> </project>

c. Execute ant command d. Files (source and class files) will be created with the package name as specified by targetNamespace in wsdl file. HelloIF.java MyHelloService.java Another way of using wsimport is using the command wsimport <WSDL path> wsimport resides in C:\Sun\jwsdp-2.0\jaxws\bin path. Note: (A) If you encounter following error on executing ant. [wsimport] error: rpc/encoded wsdls are not supported in JAXWS 2.0. (i) (ii) Remove encodingStyle attribute from soap:body tag Replace use="encoded" with use="literal".

(B) To specify source (for .java files) and destination (for .class files) directories, specify below attributes under wsimport tag in build.xml file sourcedestdir="" Author: Ravindra V Page 4

destdir="" (C) To specify a new package name for the generated files (example dynamicproxy in below file), add below attribute under wsimport tag package=dynamicproxy (D) If you encounter below error on executing ant, this means wsimport cannot be used for a WSDL which rpc/encoded. It works only with rpc/lit and doc/lit WSDLs. This generally happens when a WSDL is created for a dotnet web service. error: undefined simple or complex type 'soapenc:Array' 2. Client program
package client; import import import import import import java.net.URL; javax.xml.rpc.Service; javax.xml.rpc.JAXRPCException; javax.xml.namespace.QName; javax.xml.rpc.ServiceFactory; dynamicproxy.HelloIF;

public class HelloDPClient { public static void main(String[] args) { try { String UrlString = "http://localhost:7001/web_services/MyHelloService" + "?WSDL"; String nameSpaceUri = "urn:Foo"; String serviceName = "MyHelloService"; String portName = "MyHelloServicePort"; System.out.println("UrlString = " + UrlString); URL helloWsdlUrl = new URL(UrlString); ServiceFactory serviceFactory = ServiceFactory.newInstance(); Service helloService = serviceFactory.createService(helloWsdlUrl, new QName(nameSpaceUri, serviceName)); HelloIF myProxy = (HelloIF) helloService.getPort( new QName(nameSpaceUri, portName), HelloIF.class); System.out.println(myProxy.sayHello("Ravi"));

Author: Ravindra V

Page 5

} }

} catch (Exception ex) { ex.printStackTrace(); }

Another approach:
package client; import java.net.URL; import import import import javax.xml.rpc.Call; javax.xml.rpc.Service; javax.xml.namespace.QName; javax.xml.rpc.ServiceFactory;

public class HelloDPClient2 { public static void main(String[] args) { try { String UrlString = "http://localhost:7001/web_services/MyHelloService" + "?WSDL"; String nameSpaceUri = "urn:Foo"; String serviceName = "MyHelloService"; String portName = "MyHelloServicePort"; System.out.println("UrlString = " + UrlString); URL helloWsdlUrl = new URL(UrlString); ServiceFactory serviceFactory = ServiceFactory.newInstance(); Service helloService = serviceFactory.createService(helloWsdlUrl, new QName(nameSpaceUri, serviceName)); /** Approach 2 */ Call call = helloService.createCall(new QName(nameSpaceUri,portName)); call.setOperationName(new QName(nameSpaceUri,"sayHello")); String[] params = {"Ravi"}; String result = (String)call.invoke(params); System.out.println("result : " + result); /** Approach 2 */ } catch (Exception ex) { ex.printStackTrace(); }

} }

Note:

Author: Ravindra V

Page 6

(i)

Include all the necessary jar files from C:\Sun\jwsdp-2.0 installation. Required jars are given below :
fastinfoset (FastInfoset.jar) jaxp (jaxp-api.jar) jaxr (jaxr-api.jar, jaxr-impl.jar) jaxrpc (jaxrpc-api.jar, jaxrpc-impl.jar, jaxrpc-spi.jar) jaxws (jaxws-api.jar, jaxws-rt.jar, jaxws-tools.jar, jsr181-api.jar, jsr250api.jar) jwsdp-shared (jaas.jar, resolver.jar, xmlsec.jar) saaj (saaj-api.jar, saaj-impl.jar) sjsxp (jsr173_api.jar, sjsxp.jar) jaxb (jaxb-api.jar, jaxb-impl.jar, jaxb-xjc.jar) weblogic.jar (from weblogic)

(ii) (iii) (iv)

Do not include both weblogic.jar and webservices.jar in the build path of eclipse Java(TM) Web Services Developer Pack 2.0 require JDK 5.0, JRE 5.0 A Service object is a factory for proxies. At runtime, the client gets information about the service by looking up its WSDL. A QName object is a tuple that represents an XML qualified name. The tuple is composed of a namespace URI and the local part of the qualified name.

Dynamic Invocation Interface Client: Uses JAX-R to locate service at runtime from registry. Client also needs to set required parameters and return values.
package client; import import import import import import javax.xml.rpc.Call; javax.xml.rpc.Service; javax.xml.rpc.JAXRPCException; javax.xml.namespace.QName; javax.xml.rpc.ServiceFactory; javax.xml.rpc.ParameterMode;

public class DII { private static String qnameService = "MyHelloService"; private static String qnamePort = "HelloIF"; private static String BODY_NAMESPACE_VALUE = "urn:Foo"; private static String ENCODING_STYLE_PROPERTY = "javax.xml.rpc.encodingstyle.namespace.uri"; 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) { try {

Author: Ravindra V

Page 7

ServiceFactory factory = ServiceFactory.newInstance(); Service service = factory.createService( new QName(qnameService)); QName port = new QName(qnamePort); Call call = service.createCall(port); call.setTargetEndpointAddress("http://loca lhost:7001/web_services/MyHelloService?WSDL"); call.setProperty(Call.SOAPACTION_USE_PROPE RTY,new Boolean(true)); call.setProperty(Call.SOAPACTION_URI_PROPE RTY,""); /*this can be left empty if you are accessing a java web service from java client. However if the web service is generated in dotnet and is accessed from java program, then this argument may require value as <target namespace> + <method name> Eg: http://Service_Created/PutFile */ call.setProperty(ENCODING_STYLE_PROPERTY,U RI_ENCODING); QName QNAME_TYPE_STRING = new QName(NS_XSD, "string"); call.setReturnType(QNAME_TYPE_STRING); call.setOperationName(new QName(BODY_NAMESPACE_VALUE,"sayHello")); call.addParameter("string", QNAME_TYPE_STRING,ParameterMode.IN); String[] params = {"Ravi"}; String result = (String)call.invoke(params); System.out.println(result); } catch (Exception ex) { ex.printStackTrace(); } } }

Accessing a dotnet web service from a java program using JAXRPC

Author: Ravindra V

Page 8

Using Stubs: 1. Generate stubs.Using the below command java org.apache.axis.wsdl.WSDL2Java http://172.23.4.152/Service1.asmx?WSDL This will generate following files in the path C:\Documents and Settings\<User ID>\<Namespace> Service1.java Service1Locator.java Service1Soap12Stub.java Service1Soap.java Service1SoapStub.java 2. Write Client to access the stubs: Example 1:
package client; import Service_Created.*; public class DMSTestStub { public static void main(String [] args) { try { Service1Locator loc = new Service1Locator(); Service1Soap port = loc.getService1Soap(); System.out.println(port.getCustomerData("120")); } catch(Exception e) {System.out.println(e.getMessage());} } }

Example 2: (With reference parameters)


package client; import Service_Created.*; import javax.xml.rpc.holders.StringHolder; public class DMSTestStub { public static void main(String [] args) { try { Service1Locator loc = new Service1Locator(); Service1Soap port = loc.getService1Soap(); StringHolder s=new StringHolder(); Object obj2 = port.checkstring("Ravi",s); System.out.println(obj2); System.out.println(s.value); }

Author: Ravindra V

Page 9

catch(Exception e) {System.out.println(e.getMessage());} } }

Note: 1. Classpath should be similar to the one below:


C:\bea\jdk142_05\jre\lib;C:\bea\weblogic81\server\lib\weblogic.jar;D :\My Tutorials\J2EE\Web Services\Softwares\axis-src-1_4\axis1_4\lib\axis.jar;D:\My Tutorials\J2EE\Web Services\Softwares\axissrc-1_4\axis-1_4\lib\wsdl4j-1.5.1.jar;D:\My Tutorials\J2EE\Web Services\Softwares\axis-src-1_4\axis-1_4\lib\log4j-1.2.8.jar;D:\My Tutorials\J2EE\Web Services\Softwares\axis-src-1_4\axis1_4\lib\axis-ant.jar;D:\My Tutorials\J2EE\Web Services\Softwares\axis-src-1_4\axis-1_4\lib\commons-discovery0.2.jar;D:\My Tutorials\J2EE\Web Services\Softwares\axis-src1_4\axis-1_4\lib\commons-logging-1.0.4.jar;D:\My Tutorials\J2EE\Web Services\Softwares\axis-src-1_4\axis-1_4\lib\jaxrpc.jar;D:\My Tutorials\J2EE\Web Services\Softwares\axis-src-1_4\axis1_4\lib\saaj.jar;D:\My Tutorials\J2EE\Web Services\Softwares\axissrc-1_4\axis-1_4\lib\wsdl4j-1.5.1.jar;D:\My Tutorials\J2EE\Web Services\Softwares\axis-src-1_4\axis-1_4\lib\endorsed\xercesImpl2.6.2.jar;D:\My Tutorials\J2EE\Web Services\Softwares\axis-src1_4\axis-1_4\lib\endorsed\xml-apis-2.6.2.jar

2. Following exception is thrown while generating stubs if JDK1.4 is set is classpath : NoSuchMethodError javax.xml.namespace.QName.getPrefix Using DII: Example 1 (Without parameters):
package client; import import import import import import javax.xml.rpc.Call; javax.xml.rpc.Service; javax.xml.rpc.JAXRPCException; javax.xml.namespace.QName; javax.xml.rpc.ServiceFactory; javax.xml.rpc.ParameterMode;

public class DMSTestDII2 { private static String qnameService = "Service1"; private static String qnamePort = "Service1Soap"; // this refers to wsdl:portType tag private static String BODY_NAMESPACE_VALUE = "http://Service_Created/"; private static String ENCODING_STYLE_PROPERTY = "javax.xml.rpc.encodingstyle.namespace.uri"; private static String NS_XSD = "http://www.w3.org/2001/XMLSchema";

Author: Ravindra V

Page 10

private static String URI_ENCODING = "http://schemas.xmlsoap.org/soap/encoding/"; public static void main(String[] args) { try { ServiceFactory factory = ServiceFactory.newInstance(); Service service = factory.createService( new QName(qnameService)); QName port = new QName(qnamePort); Call call = service.createCall(port); call.setProperty(Call.SOAPACTION_USE_PROPERTY,new Boolean(true)); call.setProperty(Call.SOAPACTION_URI_PROPERTY,BODY_NAMESPAC E_VALUE+"HelloJava"); call.setTargetEndpointAddress("http://172.23.4.152/Service1 .asmx?WSDL"); call.setPortTypeName(new QName(NS_XSD, qnameService)); call.setProperty(ENCODING_STYLE_PROPERTY,URI_ENCODING); QName QNAME_TYPE_STRING = new QName(NS_XSD, "string"); call.setReturnType(QNAME_TYPE_STRING); call.setOperationName(new QName(BODY_NAMESPACE_VALUE,"HelloJava")); String[] params = {}; String result = (String)call.invoke(params); System.out.println(result); call.setProperty(Call.SOAPACTION_URI_PROPERTY,BODY_NAMESPAC E_VALUE+"HelloWorld"); call.setOperationName(new QName(BODY_NAMESPACE_VALUE,"HelloWorld")); result = (String)call.invoke(params); System.out.println(result); } catch (Exception ex) { ex.printStackTrace(); }

} }

Example 2 (With parameters):


package client; import import import import import public javax.xml.rpc.Call; javax.xml.rpc.Service; javax.xml.namespace.QName; javax.xml.rpc.ServiceFactory; javax.xml.rpc.ParameterMode; class DMSTestDII {

private static String qnameService = "Service1";

Author: Ravindra V

Page 11

private static String qnamePort = "Service1Soap"; private static String BODY_NAMESPACE_VALUE = "http://Service_Created/"; private static String ENCODING_STYLE_PROPERTY = "javax.xml.rpc.encodingstyle.namespace.uri"; 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) { try { ServiceFactory factory = ServiceFactory.newInstance(); Service service = factory.createService( new QName(qnameService)); QName port = new QName(qnamePort); Call call = service.createCall(port); call.setTargetEndpointAddress("http://172.23.4.152/Service1 .asmx?WSDL"); call.setProperty(Call.SOAPACTION_USE_PROPERTY,new Boolean(true)); call.setProperty(Call.SOAPACTION_URI_PROPERTY,"http://Servi ce_Created/SayHello"); call.setProperty(ENCODING_STYLE_PROPERTY,URI_ENCODING); QName QNAME_TYPE_STRING = new QName(NS_XSD, "string"); call.setReturnType(QNAME_TYPE_STRING); call.setOperationName(new QName(BODY_NAMESPACE_VALUE,"SayHello")); call.setPortTypeName(new QName(NS_XSD, qnameService)); call.addParameter("sInputval", QNAME_TYPE_STRING,ParameterMode.IN); // first parameter should be same as the one //implemented in dotnet web service String[] params = {"Ravi"}; String result = (String)call.invoke(params); System.out.println(result); } catch (Exception ex) { ex.printStackTrace(); }

} }

Note: There is difference in encoding types between java and dotnet. When a value is sent from java to dotnet web service (which is document literal encoded by default), the value doesnt flow to the dotnet web method and is set to null. This is because in java the encoding style is rpc by default. In order to have proper interoperability between java and

Author: Ravindra V

Page 12

dotnet, the encoding style in dotnet web method should support rpc. A sample program in C# with these changes is given below :
using System; using System.Web.Services; using System.Web.Services.Protocols; namespace Service_Created { [WebService] [SoapRpcService] public class Service1 : WebService { [WebMethod] [SoapRpcMethod] public String sayHello(string sInputval) { return Hello + sInputval; } } }

Example 3: (With multiple parameters)


package client; import import import import import import javax.xml.rpc.Call; javax.xml.rpc.encoding.XMLType; javax.xml.rpc.Service; javax.xml.namespace.QName; javax.xml.rpc.ServiceFactory; javax.xml.rpc.ParameterMode;

public class DMSTestDIIParams { private static String qnameService = "Service1"; private static String qnamePort = "Service1Soap"; // this refers to wsdl:portType tag private static String BODY_NAMESPACE_VALUE = "http://Service_Created/"; private static String ENCODING_STYLE_PROPERTY = "javax.xml.rpc.encodingstyle.namespace.uri"; 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) { try { System.setProperty( "javax.xml.rpc.ServiceFactory","weblogic.webservice .core.rpc.ServiceFactoryImpl"); ServiceFactory factory =

Author: Ravindra V

Page 13

ServiceFactory.newInstance(); Service service = factory.createService( new QName(qnameService)); QName port = new QName(qnamePort); Call call = service.createCall(port); call.setProperty(Call.SOAPACTION_USE_PROPERTY,new Boolean(true)); call.setProperty(Call.SOAPACTION_URI_PROPERTY,BODY_NAMESPAC E_VALUE+"Add_float"); call.setTargetEndpointAddress("http://172.23.4.152/Service1 .asmx?WSDL"); call.setPortTypeName(new QName(NS_XSD, qnameService)); call.setProperty(ENCODING_STYLE_PROPERTY,URI_ENCODING); QName QNAME_TYPE_FLOAT = new QName(NS_XSD, "float"); call.setReturnType(QNAME_TYPE_FLOAT); call.setOperationName(new QName(BODY_NAMESPACE_VALUE,"Add_float")); call.addParameter("num1", QNAME_TYPE_FLOAT,ParameterMode.IN); call.addParameter("num2", QNAME_TYPE_FLOAT,ParameterMode.IN); Object[] params = {25,12}; Float result = (Float)call.invoke(params); System.out.println(result.floatValue()); } catch (Exception ex) { ex.printStackTrace(); }

} }

Example 4: (With reference parameters)


package client; import import import import import import public javax.xml.rpc.Call; javax.xml.rpc.Service; javax.xml.namespace.QName; javax.xml.rpc.ServiceFactory; javax.xml.rpc.ParameterMode; java.util.List; class DMSTestDIIRef {

private static String qnameService = "Service1"; private static String qnamePort = "Service1Soap"; private static String BODY_NAMESPACE_VALUE = "http://Service_Created/"; private static String ENCODING_STYLE_PROPERTY = "javax.xml.rpc.encodingstyle.namespace.uri"; private static String NS_XSD = "http://www.w3.org/2001/XMLSchema"; private static String URI_ENCODING = "http://schemas.xmlsoap.org/soap/encoding/";

Author: Ravindra V

Page 14

public static void main(String[] args) { try { ServiceFactory factory = ServiceFactory.newInstance(); Service service = factory.createService( new QName(qnameService)); QName port = new QName(qnamePort); Call call = service.createCall(port); call.setTargetEndpointAddress("http://172.23.4.152/Service1 .asmx?WSDL"); call.setProperty(Call.SOAPACTION_USE_PROPERTY,new Boolean(true)); call.setProperty(Call.SOAPACTION_URI_PROPERTY,"http://Servi ce_Created/checkstring"); call.setProperty(ENCODING_STYLE_PROPERTY,URI_ENCODING); QName QNAME_TYPE_STRING = new QName(NS_XSD, "string"); call.setReturnType(QNAME_TYPE_STRING); call.setOperationName(new QName(BODY_NAMESPACE_VALUE,"checkstring")); call.setPortTypeName(new QName(NS_XSD, qnameService)); call.addParameter("str", QNAME_TYPE_STRING,ParameterMode.IN); call.addParameter("str2", QNAME_TYPE_STRING,ParameterMode.OUT); String[] params = {"Ravi"}; String result = (String)call.invoke(params); List l = call.getOutputValues(); System.out.println(result); System.out.println(l.get(0)); } catch (Exception ex) { ex.printStackTrace(); } } }

Accessing a dotnet web service from a java program using weblogic web services Accessing a web service using weblogic web services doesnt need much code change in the existing client programs. Only changes which are required are given below : 1. It is not required to include all the jar files in C:\Sun\jwsdp-2.0 folder but instead it is just sufficient to include weblogic.jar and webservices.jar in the classpth/buildpath. 2. Following line needs to be included at the top of the method to indicate weblogic web-services are to be used. Author: Ravindra V Page 15

System.setProperty( "javax.xml.rpc.ServiceFactory","weblogic.webserv ice.core.rpc.ServiceFactoryImpl" );

By doing the above changes same client programs will work. DII example is given below:
package client; import import import import javax.xml.rpc.Call; javax.xml.rpc.Service; javax.xml.namespace.QName; javax.xml.rpc.ServiceFactory;

public class DMSTestDIIHello { private static String qnameService = "Service1"; private static String qnamePort = "Service1Soap"; private static String BODY_NAMESPACE_VALUE = "http://Service_Created/"; private static String ENCODING_STYLE_PROPERTY = "javax.xml.rpc.encodingstyle.namespace.uri"; 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) { try { System.setProperty( "javax.xml.rpc.ServiceFactory","weblogic.webserv ice.core.rpc.ServiceFactoryImpl" ); ServiceFactory factory = ServiceFactory.newInstance(); Service service = factory.createService( new QName(qnameService)); QName port = new QName(qnamePort); Call call = service.createCall(port); call.setProperty(Call.SOAPACTION_USE_PROPERTY,new Boolean(true)); call.setProperty(Call.SOAPACTION_URI_PROPERTY,BODY_NAMES PACE_VALUE+"HelloJava"); call.setTargetEndpointAddress("http://172.23.4.152/Servi ce1.asmx?WSDL"); call.setPortTypeName(new QName(NS_XSD, qnameService)); call.setProperty(ENCODING_STYLE_PROPERTY,URI_ENCODING); QName QNAME_TYPE_STRING = new QName(NS_XSD, "string"); call.setReturnType(QNAME_TYPE_STRING); call.setOperationName(new QName(BODY_NAMESPACE_VALUE,"HelloJava")); String[] params = {};

Author: Ravindra V

Page 16

String result = (String)call.invoke(params); System.out.println(result); call.setProperty(Call.SOAPACTION_URI_PROPERTY,BODY_NAMES PACE_VALUE+"HelloWorld"); call.setOperationName(new QName(BODY_NAMESPACE_VALUE,"HelloWorld")); Object result2 = call.invoke(params); System.out.println(result2); } catch (Exception ex) { ex.printStackTrace(); }

} }

Comparison
Client Access Modes Stub Approach J2EE clients should use generated stubs to access services. Dynamic Proxy Unlike stubs, the client developer needs only the client-side interface that matches the service endpoint interface. DII A client dynamically accesses a Web service by locating the service at runtime from a JAXR registry at runtime. Generally, using DII is more difficult for a developer. It is more prone to class cast exceptions. A developer may choose to use the DII approach when a complete WSDL document is not available or provided, particularly when the WSDL document does not specify ports. The DII approach may have slower access.

When to consider

Stub communication easily allows Java access to a Web service. This is the recommended approach for accessing services and their WSDL files when they are unlikely to change over time.

Consider using the dynamic proxy approach if portability is important to your application, since this approach uses the service's endpoint interface to communicate with a service at runtime. Dynamic proxy communication is the most portable mode across JAX-RPC implementations.

Performance

Faster

Because of how they access a service at runtime, dynamic proxies may have additional overhead

Author: Ravindra V

Page 17

when calls are made. Setting parameters and return values Requires WSDL Not required. Taken care in WSDL. No. A service endpoint interface may generate a stub class along with information concerning the protocol binding. Not required. Taken care in WSDL. Client needs to set required parameters and return values. No. Calls may be used when partial WSDL or no WSDL is specified. Use of methods other than the createCall() method on the Call interface may result in unexpected behavior in such cases. No

No. A partial WSDL (one with the service port element undefined) may be used.

Requires Yes generation of code using a tool Ability to programmatically change the service endpoint URL Yes, but requires regeneration of stubs.

No

Yes, but the client-side service endpoint interface must match the representation on the server side.

Yes

Data types

It is recommended to use the static stubs programming model if schema types other than primitive types are used in the WSDL file. Simple and easy to code

This invocation mode should be used when the WSDL contains primitive schema types only.

This invocation mode should be used when the WSDL contains primitive schema types only.

Advantages

Requires no stub generation. Supports a service endpoint interface dynamically at runtime.

No impact of changes on the client side. DII can call a remote procedure even if the signature of the remote procedure or the name of

Author: Ravindra V

Page 18

the service is unknown until runtime. Disadvantages Change of web service definition leads to regeneration of stubs. It is implementation specific. Cannot be used for non-primitive data types. Much more coding is involved. Difficult to debug and test. Cannot be used for nonprimitive data types.

Conclusion: The reason why WINS cannot use dynamic proxies is that it requires creation of client artifacts (a java class which will contain path to WSDL). And these cannot be created if the methods have array as parameter (like byte[]). It results in following error error: undefined simple or complex type 'soapenc:Array' The reason being ant task wsimport cannot be used with RPC encoding. Even though dotnet uses document encoding by default, we need to change this to RPC encoding for Java to Dotnet interoperability. Hence best approach would be to use DII.

Author: Ravindra V

Page 19

Das könnte Ihnen auch gefallen