Pages

Saturday, March 19, 2011

Axis1 SUNspecification. JAX-RPC JDK 1.4

Enabling the apache-axis1 engine in tomcat.
Download: axis-src-1_4
Just a sample example:
----------------------
Provider:
1)Create a dynamic web-project say. AxisLab1
2)Write the services that you want to expose.
Note:can also posses internal usage of custom beans.
3)Replace the existing web.xml with the one obtained from the axis.
4)Add the server-config.wsdd and update with the service information that is exposed.
5)Add i18n_ja.properties,i18n.properties and the .jsp pages.
6)Add the 11 jars..set from the buildpath the axis-src-1.4 jar files
7)Restart the server.

Client Side:
=============
1)creat the java project say:ClientAxisLab1
2)Add the 11 jars from the lib folder of axis-src-1_4
3)Write the client code and test it



Steps Provider Side:
--------------------
1)Project Name:AxisLab1
2)package com.tcs.address;
public class Address {
public City getAddress(String city1) {
System.out.println(city1 + "service side");
City city = new City();
city.setCity(city1);
return city;
}
}
Bean class:
------------
package com.tcs.address;

public class City {

String city;
String state = "KA";

public City() {
// TODO Auto-generated constructor stub
}

public void setCity(String city) {
System.out.println("*********" + city);
this.city = city;
}

public String getCity() {
return city;
}

public void setState(String state) {
this.state = state;
}

public String getState() {
return state;
}

}
3)Replace with the web.xml as said above.
4)Add the following in the server-config.wsdd



5)Add i18n_ja.properties,i18n.properties and the .jsp,.jws pages, that you see from "axis" folder.
6)Add the 11 jar files.
7)Restart the server.
Note:
------- This is similar to registering the .wsdd from the command line.
i.e say we have some "deploy.wsdd" file slr to server-config.wsdd,from the command
prompt we can register as:
java org.apache.axis.client.AdminClient -lhttp://:/axis/services/AdminService deploy.wsdd

Steps for Client Side Code:
---------------------------
1)Create a project say:ClientAxisLab1
2)Add the 11 jars
3)Client Code:
Client.java:
============
package com.tcs.client.axis1;

import java.net.MalformedURLException;
import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.encoding.XMLType;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;

public class Client {
public static void main(String[] args) {
Service service = new Service();
String endPoint = "http://localhost:8080/AxisLab1/services/address";
try {
Call call = (Call) service.createCall();
try {
call.setTargetEndpointAddress(new java.net.URL(endPoint));
call.setOperationName("getAddress");
String city = "bangalore";
QName qName = new QName("ns1:City", "city");
BeanSerializerFactory ser = new BeanSerializerFactory(
City.class, qName);
BeanDeserializerFactory deser = new BeanDeserializerFactory(
City.class, qName);

call.registerTypeMapping(City.class, qName, ser, deser);
call.addParameter("city", qName, ParameterMode.IN);
call.setReturnType(qName);

try {
City cty = (City) call.invoke(new Object[] { city });
System.out.println(cty.getCity());
System.out.println(cty.getState());
/*
* String cty = (String) call.invoke(new Object[]{city});
* System.out.println(cty+"testst");
*/

} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

City.java
---------
package com.tcs.client.axis1;

public class City {

String city;
String state;
public City() {
// TODO Auto-generated constructor stub
}

public void setCity(String city) {
this.city = city;
}
public String getCity() {
return city;
}

public void setState(String state) {
this.state = state;
}
public String getState() {
return state;
}

}

Run the Client.java.

oops!! you will get an error:
java.io.IOException: No serializer found for class com.tcs.address.City in registry org.apache.axis.encoding.TypeMappingDelegate@9b2a51
-------------
AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
faultSubcode:
faultString: org.xml.sax.SAXParseException: Premature end of file.
faultActor:
faultNode:
faultDetail:
{http://xml.apache.org/axis/}stackTrace:org.xml.sax.SAXParseException: Premature end of file.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:215)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:386)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:316)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:230)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:798)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1242)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:375)
at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)

How to resolve????
Reason:We haven't add the beanMapping (City.java).In general axis will take care of the serialization and deserialization between the exposed service and the client data(SOAP xml data),for the custom beans we need to tell the engine/wsManager which Java classes map with which XMLtypes.
so add the following before the end of the service tag :


beanMapping qname="ns1:City" xmlns:ns1="http://com.tcs.address1"
languageSpecificType="java:com.tcs.address.City"/>

now restart the tomcat and test from the client.
o/p:
bangalore
KA


Thanks/-
keely

No comments:

Post a Comment