Solved

writing a java webservice

Posted on 2011-03-17
12
497 Views
Last Modified: 2012-05-11
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
Comment
Question by:royjayd
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35161366
0
 

Author Comment

by:royjayd
ID: 35161663
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
 
LVL 47

Accepted Solution

by:
for_yan earned 300 total points
ID: 35162302

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
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35162877
@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
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 100 total points
ID: 35162907
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
 
LVL 4

Assisted Solution

by:msk_apk
msk_apk earned 50 total points
ID: 35163360
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Assisted Solution

by:yavathol
yavathol earned 50 total points
ID: 35163785
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
 

Author Comment

by:royjayd
ID: 35165059
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35167021
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
 

Author Comment

by:royjayd
ID: 35167824
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35167840
No I didn't have a chance to work with CXF.
0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 100 total points
ID: 35221077
CXF implement JAX-WS API .if you using tomcat means then its will some problem while deploying ...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now