Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 562
  • Last Modified:

writing a java webservice

one of the interview questions was
How will you develop a java webservice ?

 All i know about webservice is there are two components
actual webservice and
webserivce client

the 'Client' invokes the 'webservice' using the WSDL.

But can anyone explain how the 'web service' is implimented in java? In my scenario i have a java webservice and a java client.

I am using eclipse. What interfaces, implimentation classes do i need
to impliment the webservice?

thanks
0
royjayd
Asked:
royjayd
  • 4
  • 3
  • 3
  • +2
5 Solutions
 
for_yanCommented:
0
 
royjaydAuthor Commented:
I want to start from scratch, I don't have wsdl or anything. All I have is
eclipse ide. What implimentation classes and interfaces do I need to create to impliment
a web service for example a webservice which has a method
called getName() which returns a string 'jay'.
0
 
for_yanCommented:

OK, this is extremely minimalistic Web service - in fact you don't need even eclipse.

All you need is Tomcat running as Web service container and you need to install Axis in that Tomcat

http://axis.apache.org/axis/

Axis is implementation of Soap - installation of both Tomcat and Axis (which runs as web application under Tomcat)
is really straightforward.

After you install Axis under Tomcat the home of your Axis installation would be Tomcat_Home/webapps/axis

These are examples of the client and server parts.
This client takes two strings as command line arguments and then
goes to the server where these two strings are concatenated
and the sum is  placed into the Vector with two identicall elements - each one the
of concatenation of your original strings.
This Vector is then  returned back to the client.

This is the client
(all additional classes which you may need will be found in the axis distribution)

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.utils.Options;
import java.util.Vector;

import javax.xml.rpc.ParameterMode;

public class VectorStringCalcClient {
   public static void main(String [] args) throws Exception {
   
       String endpoint = "http://host_for_tomcat.com:8100/axis/VectorStringCalculator.jws";


// Make the call
String method = "addString";
       String i1 = new String(args[0]);
       String i2 = new String(args[1]);

       Service  service = new Service();
       Call     call    = (Call) service.createCall();

       call.setTargetEndpointAddress(new java.net.URL(endpoint));
       call.setOperationName( method );
       call.addParameter("op1", XMLType.SOAP_STRING, ParameterMode.IN);
       call.addParameter("op2", XMLType.SOAP_STRING, ParameterMode.IN);
       call.setReturnType(XMLType.SOAP_VECTOR);

       Vector ret = (Vector) call.invoke( new Object [] { i1, i2 });

       for(int j=0; j<ret.size(); j++)System.out.println((String) ret.elementAt(j));

          }
}

Open in new window



Below is the code of the server part.

This code you place in the file called VectorStringCalculator.jws (mind, it should be not .java, but .jws)
and you place this file into TOMCAT_HOME/webapps/axis folder.



import java.util.*;

public class VectorStringCalculator {
  public Vector addString (String i1, String i2)
  {
  Vector v = new Vector();
  v.addElement("first: " + i1 + i2);
  v.addElement("second: " + i1 + i2);
    return v; 
  }

}

Open in new window



Then you compile the client and, I believe, you also need to restart Tomcat
and then you just run your client (of course so that you could access your server over the network:)

java VectorStringCalcClient 123 456

(of course you can run it from eclipse - just provide command line argumnents)

This is the output you'll see back on your client
first: 123456
second: 123456

Open in new window


I am sure with this example you'll be able to make a service which returns your name - it would be
even easier. I actually wrote this example, as I was interested how to return Vector - with
String it would be even easier.












 






0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Sathish David Kumar NCommented:
@royjayd,:

There are two way to develop java web services

one is top down (wsdl alredy exits you want to create web services code for that (web services client)

2nd one is buttom up of proccess
0
 
Sathish David Kumar NCommented:
I have a bad experinces in web services with eclipse IDE .

AXis having lot of problem .(axis 1 and axis 2 ) in creating web services .

Better use Jdeveloper they have (in that  jax-rpc ) web services method you can use

or brtter go with MyEclipse (that have 2 methods jax-rpc and rest )
0
 
msk_apkCommented:
you could do it through simple annotations like @WebService (for service) @WebServiceRef for client. for more details refer  http://download.oracle.com/javaee/6/tutorial/doc/ and more examples and source code can be downloaded for that tutorial.
0
 
yavatholCommented:
Easiest thing to do is to install WTP (web tools platform) if they are not included in your eclipse distribution (if you've downloaded JEE edition there is already everything there.
Go to servers, and create a sample server adapter (you'll have to download tomcat or some other server binaries beforehands).
Other way is to download some modern middleware with eclipse tools included like Oracle Weblogic with Oracle Workshop.
Later all you need to do is to click your way through the wizard - New -> WebService. It will generate both sides (server part and client part)
0
 
royjaydAuthor Commented:
Ok thx for the responses. For_yan, vectorstringcalculator.jws file, how is jws file different from java file? I am assuming it's a java file? Is this technology specific? Do all webservice technologies like
axis or cxf use .jws file?
0
 
for_yanCommented:
It is not different from .java file - the contents is exactly like java - you just chnage the extension - and that
gives axis the idea what to do with it.
Perhaps, it is not the only way - I found it very convenient.
0
 
royjaydAuthor Commented:
ok thanks for sharing that. It helps. Have you worked with apache CXF? I am guessing is it just like axis but very popular , not sure why
0
 
for_yanCommented:
No I didn't have a chance to work with CXF.
0
 
Sathish David Kumar NCommented:
CXF implement JAX-WS API .if you using tomcat means then its will some problem while deploying ...
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now