This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Services and the fly likes Consuming Webservice in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Consuming Webservice in Java" Watch "Consuming Webservice in Java" New topic
Author

Consuming Webservice in Java

Ranganath .S.Junpal
Ranch Hand

Joined: Jan 16, 2005
Posts: 52
Hi all,

i am trying to consume .net Webservice in the Java. I have Tomcat4.1 and axis installed .
The .net webservice just returns string "HelloWorld".
i downloaded the .wsdl file from the IIS webserver. and used the wsdl2java tool to convert to the .java files.
1)Service1.java
2)Service1Locator.java
3)Service1Soap.java
4)Service1SoapStub.java

The wsdl created from the "http://localhost/Jweb/Service1.asmx?WSDL" is as follows:-
===========================================================================
<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:s0="http://tempuri.org/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" targetNamespace="http://tempuri.org/" xmlns="http://schemas.xmlsoap.org/wsdl/">
<types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="HelloWorld">
<s:complexType />
</s:element>
<s:element name="HelloWorldResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</types>
<message name="HelloWorldSoapIn">
<part name="parameters" element="s0:HelloWorld" />
</message>
<message name="HelloWorldSoapOut">
<part name="parameters" element="s0:HelloWorldResponse" />
</message>
<portType name="Service1Soap">
<operation name="HelloWorld">
<input message="s0:HelloWorldSoapIn" />
<output message="s0:HelloWorldSoapOut" />
</operation>
</portType>
<binding name="Service1Soap" type="s0:Service1Soap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
<operation name="HelloWorld">
<soap peration soapAction="http://tempuri.org/HelloWorld" style="document" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
</binding>
<service name="Service1">
<port name="Service1Soap" binding="s0:Service1Soap">
<soap:address location="http://localhost/Jweb/Service1.asmx" />
</port>
</service>
</definitions>
===========================================================================
1)Service1.java which is as follows
===========================================================================
package org.tempuri;

public interface Service1 extends javax.xml.rpc.Service {
public java.lang.String getService1SoapAddress();

public org.tempuri.Service1Soap getService1Soap() throws javax.xml.rpc.ServiceException;

public org.tempuri.Service1Soap getService1Soap(java.net.URL portAddress) throws javax.xml.rpc.ServiceException;
}
===========================================================================
2)Service1Locator.java
===========================================================================
package org.tempuri;

public class Service1Locator extends org.apache.axis.client.Service implements org.tempuri.Service1 {

// Use to get a proxy class for Service1Soap
private final java.lang.String Service1Soap_address = "http://localhost/Jweb/Service1.asmx";

public java.lang.String getService1SoapAddress() {
return Service1Soap_address;
}

// The WSDD service name defaults to the port name.
private java.lang.String Service1SoapWSDDServiceName = "Service1Soap";

public java.lang.String getService1SoapWSDDServiceName() {
return Service1SoapWSDDServiceName;
}

public void setService1SoapWSDDServiceName(java.lang.String name) {
Service1SoapWSDDServiceName = name;
}

public org.tempuri.Service1Soap getService1Soap() throws javax.xml.rpc.ServiceException {
java.net.URL endpoint;
try {
endpoint = new java.net.URL(Service1Soap_address);
}
catch (java.net.MalformedURLException e) {
throw new javax.xml.rpc.ServiceException(e);
}
return getService1Soap(endpoint);
}

public org.tempuri.Service1Soap getService1Soap(java.net.URL portAddress) throws javax.xml.rpc.ServiceException {
try {
org.tempuri.Service1SoapStub _stub = new org.tempuri.Service1SoapStub(portAddress, this);
_stub.setPortName(getService1SoapWSDDServiceName());
return _stub;
}
catch (org.apache.axis.AxisFault e) {
return null;
}
}

/**
* For the given interface, get the stub implementation.
* If this service has no port for the given interface,
* then ServiceException is thrown.
*/
public java.rmi.Remote getPort(Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException {
try {
if (org.tempuri.Service1Soap.class.isAssignableFrom(serviceEndpointInterface)) {
org.tempuri.Service1SoapStub _stub = new org.tempuri.Service1SoapStub(new java.net.URL(Service1Soap_address), this);
_stub.setPortName(getService1SoapWSDDServiceName());
return _stub;
}
}
catch (java.lang.Throwable t) {
throw new javax.xml.rpc.ServiceException(t);
}
throw new javax.xml.rpc.ServiceException("There is no stub implementation for the interface: " + (serviceEndpointInterface == null ? "null" : serviceEndpointInterface.getName()));
}

/**
* For the given interface, get the stub implementation.
* If this service has no port for the given interface,
* then ServiceException is thrown.
*/
public java.rmi.Remote getPort(javax.xml.namespace.QName portName, Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException {
if (portName == null) {
return getPort(serviceEndpointInterface);
}
String inputPortName = portName.getLocalPart();
if ("Service1Soap".equals(inputPortName)) {
return getService1Soap();
}
else {
java.rmi.Remote _stub = getPort(serviceEndpointInterface);
((org.apache.axis.client.Stub) _stub).setPortName(portName);
return _stub;
}
}

public javax.xml.namespace.QName getServiceName() {
return new javax.xml.namespace.QName("http://tempuri.org/", "Service1");
}

private java.util.HashSet ports = null;

public java.util.Iterator getPorts() {
if (ports == null) {
ports = new java.util.HashSet();
ports.add(new javax.xml.namespace.QName("Service1Soap"));
}
return ports.iterator();
}

}

===========================================================================
3)Service1Soap.java
===========================================================================
package org.tempuri;

public interface Service1Soap extends java.rmi.Remote {
public java.lang.String helloWorld() throws java.rmi.RemoteException;
}

===========================================================================
4)Service1SoapStub.java
===========================================================================
package org.tempuri;

public class Service1SoapStub extends org.apache.axis.client.Stub implements org.tempuri.Service1Soap {
private java.util.Vector cachedSerClasses = new java.util.Vector();
private java.util.Vector cachedSerQNames = new java.util.Vector();
private java.util.Vector cachedSerFactories = new java.util.Vector();
private java.util.Vector cachedDeserFactories = new java.util.Vector();

static org.apache.axis.description.OperationDesc [] _operations;

static {
_operations = new org.apache.axis.description.OperationDesc[1];
org.apache.axis.description.OperationDesc oper;
oper = new org.apache.axis.description.OperationDesc();
oper.setName("HelloWorld");
oper.setReturnType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
oper.setReturnClass(java.lang.String.class);
oper.setReturnQName(new javax.xml.namespace.QName("http://tempuri.org/", "HelloWorldResult"));
oper.setStyle(org.apache.axis.enum.Style.WRAPPED);
oper.setUse(org.apache.axis.enum.Use.LITERAL);
_operations[0] = oper;

}

public Service1SoapStub() throws org.apache.axis.AxisFault {
this(null);
}

public Service1SoapStub(java.net.URL endpointURL, javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {
this(service);
super.cachedEndpoint = endpointURL;
}

public Service1SoapStub(javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {
if (service == null) {
super.service = new org.apache.axis.client.Service();
} else {
super.service = service;
}
}

private org.apache.axis.client.Call createCall() throws java.rmi.RemoteException {
try {
org.apache.axis.client.Call _call =
(org.apache.axis.client.Call) super.service.createCall();
if (super.maintainSessionSet) {
_call.setMaintainSession(super.maintainSession);
}
if (super.cachedUsername != null) {
_call.setUsername(super.cachedUsername);
}
if (super.cachedPassword != null) {
_call.setPassword(super.cachedPassword);
}
if (super.cachedEndpoint != null) {
_call.setTargetEndpointAddress(super.cachedEndpoint);
}
if (super.cachedTimeout != null) {
_call.setTimeout(super.cachedTimeout);
}
if (super.cachedPortName != null) {
_call.setPortName(super.cachedPortName);
}
java.util.Enumeration keys = super.cachedProperties.keys();
while (keys.hasMoreElements()) {
java.lang.String key = (java.lang.String) keys.nextElement();
_call.setProperty(key, super.cachedProperties.get(key));
}
return _call;
}
catch (java.lang.Throwable t) {
throw new org.apache.axis.AxisFault("Failure trying to get the Call object", t);
}
}

public java.lang.String helloWorld() throws java.rmi.RemoteException {
if (super.cachedEndpoint == null) {
throw new org.apache.axis.NoEndPointException();
}
org.apache.axis.client.Call _call = createCall();
_call.setOperation(_operations[0]);
_call.setUseSOAPAction(true);
_call.setSOAPActionURI("http://tempuri.org/HelloWorld");
_call.setEncodingStyle(null);
_call.setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR, Boolean.FALSE);
_call.setProperty(org.apache.axis.AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
_call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
_call.setOperationName(new javax.xml.namespace.QName("http://tempuri.org/", "HelloWorld"));

setRequestHeaders(_call);
setAttachments(_call);
java.lang.Object _resp = _call.invoke(new java.lang.Object[] {});

if (_resp instanceof java.rmi.RemoteException) {
throw (java.rmi.RemoteException)_resp;
}
else {
extractAttachments(_call);
try {
return (java.lang.String) _resp;
} catch (java.lang.Exception _exception) {
return (java.lang.String) org.apache.axis.utils.JavaUtils.convert(_resp, java.lang.String.class);
}
}
}

}

===========================================================================

I wrote a simple programe to invoke this webservice which is as follows

package ws;
import org.tempuri.Service1Locator;
import org.tempuri.Service1Soap;
public class HelloWorld {
public static void main(String args[])
{
try
{
Service1Locator loc=new Service1Locator();
Service1Soap yo=loc.getService1Soap();
System.out.println(yo.helloWorld());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

This fires an exception which is as follows
===========================================================================
AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
faultSubcode:
faultString: java.lang.NullPointerException
faultActor:
faultNode:
faultDetail:
{http://xml.apache.org/axis/}stackTrace: java.lang.NullPointerException
at java.util.Hashtable.put(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.setProperty(Unknown Source)
at org.apache.axis.encoding.DeserializationContextImpl.parse(DeserializationContextImpl.java:246)
at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:538)
at org.apache.axis.Message.getSOAPEnvelope(Message.java:376)
at org.apache.axis.client.Call.invokeEngine(Call.java:2583)
at org.apache.axis.client.Call.invoke(Call.java:2553)
at org.apache.axis.client.Call.invoke(Call.java:2248)
at org.apache.axis.client.Call.invoke(Call.java:2171)
at org.apache.axis.client.Call.invoke(Call.java:1691)
at org.tempuri.Service1SoapStub.helloWorld(Service1SoapStub.java:99)
at ws.HelloWorld.main(HelloWorld.java:25)


java.lang.NullPointerException
at org.apache.axis.AxisFault.makeFault(AxisFault.java:129)
at org.apache.axis.client.Call.invoke(Call.java:2251)
at org.apache.axis.client.Call.invoke(Call.java:2171)
at org.apache.axis.client.Call.invoke(Call.java:1691)
at org.tempuri.Service1SoapStub.helloWorld(Service1SoapStub.java:99)
at ws.HelloWorld.main(HelloWorld.java:25)
Caused by: java.lang.NullPointerException
at java.util.Hashtable.put(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.setProperty(Unknown Source)
at org.apache.axis.encoding.DeserializationContextImpl.parse(DeserializationContextImpl.java:246)
at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:538)
at org.apache.axis.Message.getSOAPEnvelope(Message.java:376)
at org.apache.axis.client.Call.invokeEngine(Call.java:2583)
at org.apache.axis.client.Call.invoke(Call.java:2553)
at org.apache.axis.client.Call.invoke(Call.java:2248)
... 4 more
===========================================================================


please let me know wht might be the problem

Thank you
Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Consuming Webservice in Java
 
Similar Threads
Axis 1.4 Problem with DIME attachment
axis -ClassCastException...Pls help
parameters of web services operation - Java.lang.object
unable to run the Axis client
Axis1 (1.4) - Simple client proof of concept - SAXParserException: Content is not allowed in prolog