Thursday, May 31, 2012

Sample Java Web service and the Client to call the same

Sample Web service and the Client to call the same

In this blog we would discuss how to develop webservice , publish the webservice programatically and then develop the client to call the above webservice. 
You need Eclipse or Jdeveloper as the IDE 

step1

First develop the interface for the webservice as shown below

package example;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style=Style.RPC)
public interface HelloWorld {
   
    @WebMethod
    String getHelloWorldAsString(String name);

}


step2

Now implement the above interface as shown below

package example;

import javax.jws.WebService;

@WebService(endpointInterface="example.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
    public String getHelloWorldAsString(String name) {
       
        return "hello world from jax -ws "+name;
    }

}

step3

We need to publish the above webservice so that the WSDL is generated and published in the given location.

package example;

import javax.xml.ws.Endpoint;

public class HelloWorldPublisher {

    /**
     * @param args
     */
    public static void main(String[] args) {
        HelloWorldImpl obj = new HelloWorldImpl();
        Endpoint.publish("http://localhost:8080/example/hello",obj);
        System.out.println("web service published ..");
       
    }

}

step4

Develop the client code to call the above published webservice

package example;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class HelloWorldClient {

    /**
     * @param args
     * @throws MalformedURLException
     */
    public static void main(String[] args) throws MalformedURLException {
       
       
        URL url = new URL("http://localhost:8080/example/hello?wsdl");
       
       
        QName qname = new QName("http://example/","HelloWorldImplService");
//note: http://example is target namespace of WSDL and HelloWorldImplService is again taken from //WSDL
       
        //1st argument is the service uri. refer to the wsdl document
        //2nd argumnet is the service name. refer to wsdl
       
        Service service = Service.create(url,qname);
        HelloWorld hello = service.getPort(HelloWorld.class);
        System.out.println(hello.getHelloWorldAsString("Aleem"));
       

    }

}

7 comments:

malini ecorp said...

Ecorptrainings.com provides J2EE in hyderabad with best faculties on real time projects. We give the best online trainingamong the J2EE in Hyderabad. Classroom Training in Hyderabad India

for IT the said...

Java comparatively less complicated now Java Courses details J2EE Training in Chennai The coverage of this quality course Asynchronous support java j2ee training institutes in chennai database operation or webservice operation Java SE & java EE Courses Training Java EE Training

for IT the said...

Java Web Services Online Training | Web Services Course | Web Services Training Courses | Java Web Services Training in Chennai | Java Training in Chennai | Java Training Institutes in Chennai | Java Web Services Interview Questions | Java Web Services Interview Questions

Devi.Angularjs said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

Online Java Training Java Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai

for IT the said...

Java Training Institutes Java Training Institutes Java EE Training in Chennai Java EE Training in Chennai Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

jazz said...

Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your..
Vmware Training in Chennai

Nandhini said...

Thanks for taking time to share this post.It is really useful.Continue sharing more like this.
Regards,
Java Training in Chennai