Codebusters

Sample Programs For Various Features

Making JAX-WS Web service using WebLogic 10

Web services are a mechanism to invoke services over web application. The best part of web-services is the transfer mechanism. XML SOAP messages are used for transmission of messages which makes it highly optimized and speedy.

Following example creates and implements a simple web-service which can be deployed to WebLogic 10. Following are the pre requisites of the sample. All of these are freely available on net.

1. Apache Ant ( ant downloads page) We will use ant for all compilation and deployment purposes. This makes us comfortable on repeated use in future. Set the ANT_HOME path in exvironment

2. JDK (java downloads page) add the bin to the classpath

3. WebLogic 10 application server (bea download) set WL_HOME to the installation directory.

4. xalan api. (xalan) some jars are needed for running the client on webservices.

Procedure -
Choose a directory to work upon and create src and lib folders. Make sure ANT_HOME is set and ant is working.
Open a command prompt and set weblogic environment by running setWLSEnv.cmd from %WL_HOME%\server\bin directory

Create a service program in the src folder. I am creating webservice.Greet class. The folder structure should be according to the packages. Eg. Greet.java will be kept in src\webservice\ folder.

//Greet.java
package webservice;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(serviceName=”Greeting_Service”, targetNamespace=”http://harsh.oracle”)
public class Greet {

@WebMethod(operationName = “sayHi”)
public String sayHi(RequestDetail detail)
{
StringBuffer buffer = new StringBuffer(“Message is \n”);
buffer.append(“\nName – ” + detail.getName());
buffer.append(“\nAddress – ” + detail.getAddress());
buffer.append(“\nMail – “+detail.getMail());
buffer.append(“\nPhone – “+ detail.getPhone());

System.out.println(buffer.toString());
return buffer.toString();
}
}

———————————————————————————————-
Annotations specify the name with which service will be exposed to the clients. We can specify the service name and operation name along with name space. Else compiler takes the default argument which constructs a name according to the method names.

Constructors of exposed service should not contain any arguments. Default constructors are used. They may or may not be defined.

I am using RequestDetail.java so that example also constitutes complex type arguments.
————————————————————————————————–
//RequestDetail.java
package webservice;

public class RequestDetail {
private String Name;
private String Address;
private String mail;
private String phone;

public RequestDetail(String Name, String Address, String mail, String phone) {
this.Name= Name;
this.Address= Address;
this.mail= mail;
this.phone= phone;
}

public RequestDetail() {}

public void setName(String Name) {
this.Name = Name;
}

public void setAddress(String Address) {
this.Address = Address;
}

public void setMail(String mail) {
this.mail = mail;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getName() {
return this.Name;
}

public String getAddress() {
return this.Address;
}

public String getMail() {
return this.mail;
}

public String getPhone() {
return this.phone;
}
}

——————————————————————————————-
This creates a source for our web service. Applications can be extended to heavy projects as per requirements.

Now its time to start with build.xml This file is read by ant for firing of the targets.
Place this build.xml along with src and lib directory.

I will explain each segment in following part.

build.xml

The starting tag specifies the project name, base directory of execution and default target.
If we want to run a desired target we add the name of the target along with the ant clause
———————————————————————————-
eg ant build

The init target sets the property names of variables used throughout the build. This is useful as in case of changing the directory names only init target needs to be updated.

Target build-setup creates the build directory if it is not created so far. Depends argument specifies that init target is fired first so the property variables are initialized.

Make sure you have executed setWLSEnv from %WL_HOME%\server\bin directory.

Target ws-compile compiles the webservice in the specified format. JWSC needs to defined before use. For this we need %WL_HOME%\server\lib\weblogic.jar This will compile and keep the deployment dependent files to the build directory. Deployment descriptors are automatically created.

Start weblogic server and be prepared to deploy the file.
wldeploy again needs to be defined as it is weblogic specific.

For this example. Server stands up on localhost port 7001 and username and password are unchanged. weblogic is default case.
Deployment name will be same as application name
Source can be given as ${bld.dir} also. By giving the war name is like making it specific.

If the service is deployed properly, a WSDL is generated automatically and will be placed at http://localhost:7001/Greet/Greeting_Service?WSDL

This WSDL file contains the service description and names by which it is exposed. If you watch carefully, portname tag contains the service name, operation name contains method names, and message elements represents the argument types.

Now it is time to make a client and test the service. We will create a proxy for this service and use a java client to invoke it.

build-client target builds the client for the service. It uses a clientgen task which is defined manually. We can specify package name of the proxy. I kept it as client.proxy

Now will make a testClient which will invoke the method in client proxy
I am keeping testclient.java in Client-prog directory. Every client build target copies the java file to the client directory and compiles the integration.

Here is the file..

//testClient.java
package client;

import client.proxy.*;

public class testClient {
public static void main(String args[]) throws Exception {

RequestDetail detail = new RequestDetail();
detail.setName(“harsh”);
detail.setAddress(“btm”);
detail.setMail(“hm@oracle.com”);
detail.setPhone(“246275″);

GreetingService gs =new GreetingService();
String ret = gs.getGreetPort().sayHi(detail);

System.out.println(“Message from web service – “+ret);
}
}

————————————————————————————————-

run-client target executes the client. Copy xalan.jar , serializer.jar , xercesImpl.jar , xml-apis.jar from xalan download to the lib directory as there are dependencies on them.

Message will be prompted. to the client side as well as the server log (prompt on which server is running) as we have given a print statement in the Greet.java for the details.

:)

Note:

Here are the special cases which could be a point while making webservices.

1. Making single war for multiple applications

Above example works fine for a single file. But if there are more then one java classes exposed as web application, every jws in ant target makes a different war. If we want to put all the stuff in a single war, following snippet gives the example.

<module name=”wsgen” contextPath=”wsgen”>
<jwsfileset srcdir=”${ws.src.dir}”>
<include name=”**/*WS.java”/>
</jwsfileset>
</module>

Every module is taken in a different jar. Hence, creates simplicity of use.

2. Referred libraries

In case exposed services has dependencies other than specified src folder, They can be kept in APP-INF/lib directory of the ear build (build directory of webservice ws-bld in this example). Dependencies should be bundled up in jar. No other reference is needed for exposing the libraries.

3. Setting Context Root

Context Root can be set by adding an element at the time of compilation of service.

It can be done by adding WLHttpTransport element to jws where we specify the file containing web service definition to be exposed. Here is a sample usage

<WLHttpTransport

contextPath=”TestService” serviceUri=”TestService”

portName=”TestServicePort1″/>

About these ads

May 17, 2008 - Posted by | J2EE, JAVA, Uncategorized, Web Services

6 Comments »

  1. Can be please send me the build file if possible for the webservice you used to build JAX-WS using weblogic.Thanks…

    Comment by Ajay | October 9, 2009 | Reply

  2. i have installed weblogic 10.3 in my machine.when i access wsdl, it is pointing to the older version weblogic 8.1 wsdl

    Comment by abirami | October 5, 2010 | Reply

    • It also depends on the way application is packaged. Make sure you are using JAX-WS while generating the application ear. There is an attribute in JWSC task which exposes the webservice as JAX-WS. By default, it makes it as JAX-RPC which is older version of webservices.
      Please make sure you are deploying the webservice as JAX-WS webservice.

      Comment by computers2007 | October 5, 2010 | Reply

    • WSDL is a generic standard for webservices and its not specific to weblogic version. Can you please explain further.

      Comment by Harsh | January 19, 2012 | Reply

  3. I have deployed a jax-ws in weblogic portal web project but when try to access the service, WebServiceContext is always coming as null. If i deploy web service as a separate WAR file, i can get value in WebServiceContext. I am using Weblogic 10.2 portal server.

    Comment by rajesh | November 10, 2010 | Reply

  4. Thanks a lot for providing this step by step explanation. It has been extremely useful for me

    Comment by Anarghya | January 15, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: