Creating a Web Service on Weblogic
With Eclipse, we can create and deploy a Web Service in a few minutes. In this tutorial, we will build a service called Document Manager, which enables users to store and retrieve documents on a server.
This tutorial will show you how to do the following tasks.
To prepare the development environment, follow the guide “Configure the Weblogic server in Eclipse”
Let's name it “MyWebService”.
Choose the target runtime (Weblogic).
Associate it with an EAR project. The EAR project will be created automatically. It is used for define deployment configuration.
Once the Web Service and EAR projects are created, attach the EAR project with the server.
(Right click on the server icon→ Add or remove projects).
To create a Web Service, simply create a Java interface and its implementation. Add Web Service annotations on the Java code to export it as a Web Service.
This interface defines two operations : findDocument and saveDocument.
package demo; import java.rmi.RemoteException; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface IDocManager { @WebMethod public Document findDocument( @WebParam(name = "documentName") String documentName) throws RemoteException; @WebMethod public void saveDocument(@WebParam(name = "document") Document document) throws RemoteException; } |
It must support serialization and must have no argument constructor.
package demo; import java.io.Serializable; import java.util.Date; public class Document implements Serializable { private String name; private byte[] content; private Date lastModification; public Document() { } public byte[] getContent() { return content; } public Date getLastModification() { return lastModification; } public String getName() { return name; } public void setContent(byte[] content) { this.content = content; } public void setLastModification(Date lastModification) { this.lastModification = lastModification; } public void setName(String name) { this.name = name; } } |
In this implementation, we use a constant to define the root directory to store documents ({user_home_directory}/Documents/file).
In future tutorials, I will show how to add new features to the service (change the implementation to use Database store, use JMX to dynamically configure the root storage directory)
package demo.impl; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.rmi.RemoteException; import java.util.Date; import javax.jws.WebService; import demo.Document; import demo.IDocManager; @WebService(endpointInterface = "demo.IDocManager") public class DocManagerImpl implements IDocManager { public static final String ROOT_DIR = System.getProperty("user.home") + "/Documents/file"; @Override public Document findDocument(String documentName) throws RemoteException { if (null == documentName || documentName.isEmpty()) { throw new RuntimeException("Invalid param : documentName=null"); } File file = new File(ROOT_DIR, documentName); Document result = new Document(); result.setLastModification(new Date(file.lastModified())); ByteArrayOutputStream buf = new ByteArrayOutputStream(); try { FileInputStream fis = new FileInputStream(file); int b = fis.read(); while (b != -1) { buf.write(b); b = fis.read(); } } catch (Exception e) { e.printStackTrace(); throw new RemoteException("Cound not read " + documentName); } result.setContent(buf.toByteArray()); result.setName(documentName); return result; } @Override public void saveDocument(Document document) throws RemoteException { if (null == document) { throw new RemoteException("Invalid param : document=null"); } if (null == document.getName() || document.getName().isEmpty()) { throw new RemoteException("Invalid param : document.name=null"); } if (null == document.getContent()) { throw new RemoteException("Invalid param : document.content=null"); } File file = new File(ROOT_DIR, document.getName()); try { FileOutputStream fos = new FileOutputStream(file); fos.write(document.getContent()); fos.close(); } catch (Exception e) { e.printStackTrace(); throw new RemoteException("Cannot write " + document.getName()); } } } |
To deploy the service, simply right click on the server icon, and choose the “publish” command (or Ctrl+Alt+P).
The service should be marked as “synchronized”.
Weblogic provides a simple tool to test a Web Service without coding the client program.
To use this facility, right click on the service implementation class (DocumentManagerImpl) → “Run as” → “Run on Server”).
The default server will be proposed, just click “Finish”.
Now, the build-in navigator opens the Weblogic Test Client (WTC) page (http://localhost:7001/wls_utc/).
It is possible to view the WSDL generated from the Java interface.
It is possible to invoke the two operations : findDocument and saveDocument.
To test this operation, create a file (such as “test.txt”) and store it on the root storage directory ({user_home_directory}/Documents/file).
Then on the WTC page, enter the document name (“test.txt”), and click at findDocument.
We will see the SOAP request and response.
In the response, we get the document content encoded in the base64 standard encoding.
Click at “re-invoke” to go back. To invoke an operation with a complex argument data type (Document object), we need to write the body of the SOAP message.
The tool already generates the template for us.
This is an example to create a document named “my_document.txt” with the content “string”, encoded as c3RyaW5n.
<saveDocument xmlns="http://impl.demo/"> <!--Optional:--> <document xmlns=""> <!--Optional:--> <content>c3RyaW5n</content> <!--Optional:--> <lastModification> <!--date time format: yyyy-MM-ddTHH:mm:ss--> 1999-12-25T01:00:00 </lastModification> <!--Optional:--> <name>my_document.txt</name> </document> </saveDocument> |
Now we will try to generate a client program from the WSDL of our service.
First, we create a simple Java project named “MyWebServiceClient”. Then, we use the new Web Service Client wizard.
Enter the URL of the Web Service.
Eclipse will generate the following classes :
We can now write a simple client program to invoke the Web Service.
package demo.run; import java.net.MalformedURLException; import java.net.URL; import java.rmi.RemoteException; import javax.xml.rpc.ServiceException; import demo.DocManagerImplServiceLocator; import demo.Document; import demo.IDocManager; public class RunClient { public static void main(String[] args) throws MalformedURLException, ServiceException, RemoteException { DocManagerImplServiceLocator locator = new DocManagerImplServiceLocator(); IDocManager service = locator.getDocManagerImplPort(new URL( "http://localhost:7001/MyWebService/DocManagerImplService")); String docName = "test.txt"; Document d = service.findDocument(docName); System.out.println("Found document " + docName); System.out.println("Text content of the document : " + new String(d.getContent())); } } |