You are here

Integrate J2ME Mobile Clients into Web Service 2 -- Create Web Service and J2EE Client

Blog Terms: 


2 Create Web Service


2.1 Create Web Service Project

Enter the %AXIS2_HOME%/samples (Here is D:\ProgramFiles\axis2\samples) , there are many Web Service good example here. If you are not clear about how to create one Web Service from nothing, just like me, those examples are good turorials. Now I will use one and modify it to be our first Web Service.


2.1.1 Copy Project quickstart and rename it to firstservice

You can use your own project name, but if you are one beginner like me, I suggest you had better follow me, because any change here maybe cause one Web Service exception such as wrong namespace. Once you know the workflow, you can do everything as you will.
The copied new Project should have the following structure:

- firstservice
- resources
-- META-INF
--- services.xml
- src
-- samples
--- quickstart
---- service
------ pojo
------- StockQuoteService.java
-build.xml

The next work please change the structure and file name as following, I am not advertising here, this will let you know clearly what kind of configuration should be changed later.

- firstservice
- resources
-- META-INF
--- services.xml
- src
-- www
--- easywms
---- com
----- FirstService.java
-build.xml

2.1.2 Open FirstServic.java, and modify the code as here, it is easy?


package www.easywms.com;

public class FirstService{

public String request(String name, String time) {
return "Hello, " + name + ", it is " + time;
}

}


2.1.3 Modify services.xml

Open service.xml in resources/META-INF, modify it as following:
<service name="FirstService" scope="application" typeNamespace="http://easywms.www/types"
targetNamespace="http://easywms.www/wsdl"
packageName="FirstService">
<description>
My Fisrt Service
</description>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<schema schemaNamespace="http://easywms.www/xsd"/>
<parameter name="ServiceClass">www.easywms.com.FirstService</parameter>
</service>

The service.xml file tells Axis2 about the service. It points out the class and the methods to be turned into operations. It also specifies whether the service should use a "in-out" receiver, or an "in only" receiver.
Pay attention to the Namespace(red font) in this xml configuration file, at deployment, if we do not specify a schema target
namespace, then the target namespace will be calculated using the
package name of the service implementation class. For example, if the
full qualified name of the service implementation class is www.easywms.com.FirstService then the schema namespace will be,

http://FirstService.com.easywms.www/xsd

If you want to provide your own value for that, you can do that by adding the following element into the services.xml file, just as the red font in our service.xml.
<service>
<schema schemaNamespace=http://FirstService.www/xsd/>
</service>


2.1.4 Modify build.xml

Build.xml is very important for ANT, open it and change something as following, please notice the red font, that is the difference from project quickstart:

<project name="firstservice" basedir="." default="generate.service">

<property environment="env"/>
<property name="AXIS2_HOME" value="../.."/>

<property name="build.dir" value="build"/>

<path id="axis2.classpath">
<fileset dir="${AXIS2_HOME}/lib">
<include name="*.jar"/>
</fileset>
</path>

<target name="compile.service">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.dir}/classes"/>

<!--First let's compile the classes-->
<javac debug="on"
fork="true"
destdir="${build.dir}/classes"
srcdir="${basedir}/src"
classpathref="axis2.classpath">
</javac>
</target>

<target name="generate.wsdl" depends="compile.service">
<taskdef name="java2wsdl"
classname="org.apache.ws.java2wsdl.Java2WSDLTask"
classpathref="axis2.classpath"/>
<java2wsdl className="www.easywms.com.FirstService"
outputLocation="${build.dir}"
targetNamespace="http://easywms.www/"
schemaTargetNamespace="http://easywms.www/xsd">
<classpath>
<pathelement path="${axis2.classpath}"/>
<pathelement location="${build.dir}/classes"/>
</classpath>
</java2wsdl>
</target>

<target name="generate.service" depends="compile.service">
<!--aar them up -->
<copy toDir="${build.dir}/classes" failonerror="false">
<fileset dir="${basedir}/resources">
<include name="**/*.xml"/>
</fileset>
</copy>
<jar destfile="${build.dir}/FirstService.aar">
<fileset excludes="**/Test.class" dir="${build.dir}/classes"/>
</jar>
</target>

<target name="clean">
<delete dir="${build.dir}"/>
</target>
</project>


2.1.5 Build Web Service

Run cmd and enter the path %AIXS2_HOME%/samples/firstservice/ , in this case, it is D:\ProgramFiles\axis2\samples\fisrtservice, and type to run
ant generate.service

This will create one build folder in the firstservice project with such structure:
creates the following directory
structure

- firstservice
--build
---classes
- META-INF
- services.xml
- www
- easywms
- com
- FirstService.class
--- FirstService.aar

2.1.6 Deploy Web Service

Rename the classes directory to FirstService, and copy
it to the %TOMCAT_HOME%/webapps/axis2/WEB-INF/services directory, in this case, it is D:\ProgramFiles\Tomcat6.0\webapps\axis2\WEB-INF\services. Otherwise, copy the build/FirstService.aar file to the %TOMCAT_HOME%/webapps/axis2/WEB-INF/services directory.
Then check to make sure that the service has been properly deployed
by viewing the list of services at:

http://localhost:8080/axis2/services/listServices

And you can find our first service:

FirstService
Service EPR : http://localhost:8080/axis2/services/FirstService
Service Description : FirstService
Service Status : Active
Available Operations

* request

You can also checkout the WSDL at:

http://localhost:8080/axis2/services/FirstService?wsdl

And the schema at:

http://localhost:8080/axis2/services/FirstService?xsd

Now our first service is working, to quickly test the service. Try
pointing your browser to the following URL:

http://localhost:8080/axis2/services/FirstService/request?name=Guy&time=10oclock

Now will get the following response SOAP message:

<ns:requestResponse>

<ns:return>Hello, Guy, it is 10oclock</ns:return>

</ns:requestResponse>

Till now, we have built one Web Service successfully.


2.2 Create Normal Java Web Service Client

Before we do it with J2ME client, I will create one normal Java client, so that we can know how the client works with Web Service, this is helpful, I have spent much time tolearn J2ME client and WS, and I found the normal WS client is very good tutorial for me to understand the whole structure and workflow.


2.2.1 Generate WSDL

WSDL is Web Services Description Language, Axis can use it to generate Stub java source code.
Run cmd and enter the path %AIXS2_HOME%/samples/firstservice/ , in this
case, it is D:\ProgramFiles\axis2\samples\fisrtservice, and type to run
ant generate.wsdl
This will run the task "generate.wsdl" in build.xml in 2.1.4, and generate one "FirstService.wsdl" in build folder.

Notice: we can use Axis2 Eclipse Plugins to do this. Here is the turorial.



2.2.2 Generate Stub Java Code


What is Stub? Web service stub provides replacement implementations for another Web service that the Web service under test interacts with. Now I will generate the client-side stub code used by the client to access the
Web service. The stub is class that is a local Java object that acts
as a proxy for the Web service instance. The WS client makes a local
method call to the stub and the stub, in turn, calls the Web service on
the server.

Run cmd and enter the path %AIXS2_HOME%/samples/firstservice/build/, in this case, it is D:\ProgramFiles\axis2\samples\firstservice\build, and run this:
%AXIS2_HOME%\bin\WSDL2Java -uri FirstService.wsdl -p www.easywms.com -d adb -s
(D:\ProgramFiles\axis2\bin\WSDL2Java -uri FirstService.wsdl -p www.easywms.com -d adb -s)

It will create one "src" folder with Stub Java code inside (in www/com path)

Notice: we can use Axis2 Eclipse Plugins to produce the stub code. Here is the turorial.


2.2.3 Create Client project


Open Eclipse, create one Java Project named "firstclient", create "src" as source folder and one folder with name "lib", copy the jar files in the following list from
%AIXS2_HOME%/lib (D:\ProgramFiles\axis2\lib) to firstclient/lib, and import them as Libraries in "Java Build Path"; copy the java stub file in src folder that created in 2.2.2. And your project in Eclipse will be like this image:



Now, create one class named "FirstClient" with the following code:
package www.easywms.com;

public class FirstClient {
public static void main(String[] args) {
FirstServiceStub stub;
try {
double c_value = 32;
stub = new FirstServiceStub("http://localhost:8080/axis2/services/FirstService");
FirstServiceStub.Request request = new FirstServiceStub.Request();

request.setName("Catty");
request.setTime("11 oclock");

FirstServiceStub.RequestResponse requestresponse = stub.request(request);

System.out.println("The response from FirstService: " +requestresponse.get_return());
} catch (Exception e) {
e.printStackTrace();
}
}
}

Run it as "Java Application", we will see the message from Web Service FirstService:
The response from FirstService: Hello, Catty, it is 11 oclock

Till now, we have finished one Web Service and one normal java client. Next chapter, I will build one j2ME client.
This is the source of FirstService and FirstClient.


Ref:
http://ws.apache.org/axis2/1_3/quickstartguide.html
http://ws.apache.org/axis2/1_3/userguide-creatingclients.html#choosingcl...
http://www.devx.com/wireless/Article/28046/0/page/1
http://wso2.org/library/2060
http://wso2.org/library/1719

Comments

What happen to chapter3 {:?}

Where is Client for j2ME?

There's nothing in your article that talks about J2ME client as mentioned in the title. I think its a little misleading if have it in the subject, but no where to be seen in the content.

Dear All,..

I want to invoke object from web service from j2me and using jsr 172, what is the object type should i use? I can't use String[],List(util).. need help.thx.