Click here to Skip to main content
12,746,945 members (29,074 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked
Posted 24 Jun 2011

Solving Real World Problems using Web Services

, 24 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
In this article, we will look at a real world technical problem in an enterprise and how web service helps us in solving the problem.

Real World Example of Web Service


Today in this article, we will look at a real world technical problem in an enterprise and how web service helps us in solving the problem. This is one of the main questions while appearing for Web Service interviews. You can also get videos on Java Design Pattern, J2EE, Struts 2, Hibernate, Spring, Web services using Java/JaxWS/JAX RS.

If you are new to web services, then view the following starter video for the same:


OAx organization is making use of Content management system (CMS) named Ex for storing documents pertaining to one application.

Due to the performance given by the Ex, it was decided to replace other existing applications making use of other CMS to above one.

Possible Solution 1

To achieve the above said solution, existing applications can make use of Ex API for doing operations with their documents.

Directly making CMS API had the following disadvantages:

  • Tomorrow if the above CMS tool has to be replaced by another one due to some reason, it will imply changing the code in all the applications making use of above CMS tool.
  • The Ex API has extensible support for Java but does not provide a solution for Microsoft technologies. There are many applications already developed using .NET and it is not possible to completely rewrite the same.

Web Service in Action

The above problem can be solved using Web service.

Functionalities which are to be used by various applications will be now exposed as Web service.

Since web service is based on the principle of separation between interface and implementation, the implementation part of Web service will make use of core Ex API to achieve the required functionality.

Since web service provides transparency w.r.t. programming language, applications written in various programming language can access the web service without any hindrance.

Hence using web service serves as the best solution in this scenario.

JAX WS---A Hero in Web Service World

JAX WS helps in developing web service in the Java side.

Following are the things which indicate JAX WS as the best solution:

  • Annotation driven: Ease of development using annotation
  • MTOM: Message transmission optimization mechanism which provides advantage over previous Base 64 encoding for attachments

Web Service Scenario Exposing Functionalities as Service

Implementation Details

Let's consider one of the functions to be provided by web services:

  • Insertion of PDF data in CMS (In our case, we will store it in some location instead of CMS to keep it simple). We will implement the same using Eclipse Helios. In the example, instead of storing the PDF file in CMS, we will store the same at some location in the hard drive.

Step 1: Create a Java project and name the project as DocumentProject:

Step 2: Create a DocumentInt class within a package com.questpond.service with the following method signature:

package com.questpond.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@SOAPBinding(style = Style.RPC)
public interface DocumentInt {
	@WebMethod	public String upload(byte data[]) ;

Step 3: Create a DocumentImpl class implementing DocumentInt and give body to upload. The function takes byte array and converts the same to a file at location "D:/eclipse/Example.pdf". MTOM is used to support optimization while transmitting binary data.

package com.questpond.service;
import javax.jws.WebService;
@WebService(endpointInterface = "com.questpond.service.DocumentInt")
public class DocumentImpl implements DocumentInt {
public String upload(byte data[]) {		
OutputStream  fos;
try {
	fos = new FileOutputStream(new File("D://eclipse//Example.pdf"));
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
	return "success";

Step 4: Create DocumentPublisher class which will publish the created web service. Put the following code inside the same:

package com.questpond.service;

public class DocumentPublisher {
 public static void main(String[] args) {
Endpoint.publish("http://localhost:7656/questpond/image", new DocumentImpl());
 System.out.println("Server is published!");

Step 5: Execute the publisher class so that web service is exposed. Confirm the same by putting the following URL in Web browser to get the exposed WSDL file. A snippet of the same is shown:

Step 6: Create a client for web service. Let’s name the class as Main.

The Main class will be responsible to invoke the web service and since the parameter to the upload function is a byte array which contains file data, the client will convert file to a byte array and will pass the same to upload function which will convert the byte array to file. Put the following code with main function of Main class.

public static void main(String[] args) {
		URL url;
		byte[] bytes = new byte[5000];
		try {
		url = new URL("http://localhost:7655/questpond/image?wsdl");
		QName qname = new QName("",
		Service service = Service.create(url, qname);
		DocumentInt server = service.getPort(DocumentInt.class);
		File file = new File("E:/Documents/view_pdf.pdf");
		bytes = new byte[(int) file.length()];
			try {
			FileInputStream fileInputStream = new FileInputStream(file);;
			} catch (FileNotFoundException e) {
				System.out.println("File Not Found.");
			} catch (IOException e1) {
				System.out.println("Error Reading The File.");

		} catch (Exception e) {

The above function reads a file from E:/Documents/view_pdf.pdf and converts the file to a byte array. This byte array is then used to invoke the upload function.

On executing the client, we will get a file created in the location which we specified in upload function.


Thus we have seen a real world scenario where web service is used and a partial implementation of the same. You can further enhance your knowledge on Web service by watching my videos on Web services and their implementation in Java using JAXWS and JAXRS.

Download the source code for the above article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Vishwanath Narayanan
India India
I am an architect in Java and J2EE technologies having hands on experience on variety of frameworks, libraries under Java cover.
I conduct training for java technologies like design patterns, j2ee, Struts, Hibernate, Spring, Rich Interface etc and likes to write articles on the same in free time.

You may also be interested in...


Comments and Discussions

Questionneed short description answer Pin
kvsravindrareddy5-Mar-13 7:31
memberkvsravindrareddy5-Mar-13 7:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170215.1 | Last Updated 24 Jun 2011
Article Copyright 2011 by Vishwanath Narayanan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid