Thursday, May 29, 2014

Hello World program in Web Service in Java using NetBeans IDE

Hi Guys,
Today i am going to describe you how to write a simple hello world program in web services using java in NetBeans IDE.

First we need to create the service.

For that, first create a new "Web Project" from "Java EE".

Then create an Interface class file named IHelloWOrld.java and write the below code
package com.hello;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.WebServiceException
@WebService
public interface IHelloWorld{
@WebMethod
public String hello() throws WebServiceException;
}
Then, create another class file named HelloWorld.java

Then write the code below


package com.hello; import javax.jws.WebMethod; import javax.jws.WebService; import javax.xml.ws.WebServiceException; @WebService public class HelloWorld implements IHelloWorld{ @WebMethod public String hello() throws WebServiceException{ return "Hello World"; } }
After that, create another project either standalone or a web project for service consumer/client side.
Here I am describing as a standalone application.
Then create a Web Service Client file and give the project name of the above service provider and package same as the above which we gave as "com.hello" and click on finish.
Then the stubs are generated for u to develop the consumer code or application.
Then create a class file and write the below code.
import com.java.HelloWorldService;
import com.java.HelloWorldPort;

public class HelloWorldClient{
public static void main(String args[]){
HelloWorldService service=new HelloWorldService();
HelloWorldPort port=service.getHelloWorldPort();
String hello=port.hello();
System.out.println(hello);
}
}

No comments:

Post a Comment