Create a web service and client in 5 minutes with JAX-WS

mkdir abc
vi abc/DoStuff.java
package abc;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class DoStuff {
   public double squareMe(double iNumber) {
       return iNumber*iNumber;
   }
   public static void main(String[] args) {
        Endpoint.publish("http://localhost:8081/dostuff",
           new DoStuff());
   }
}

The following 2 commands do not produce any output (unless you have errors):

javac abc/DoStuff.java
wsgen -cp . abc.DoStuff

The following command starts the web service so it has to keep running, so you’ll have to start up another command line session while it runs

java abc.DoStuff

Let’s build the client for the web service:

wsimport -p client -keep http://localhost:8081/dostuff?wsdl
vi client/MyApp.java
package client;
class MyApp {
   public static void main(String[] args) {
       DoStuffService sv = new DoStuffService();
       DoStuff prx = sv.getDoStuffPort();
       double res = prx.squareMe(11);
 System.out.println("result=" + res);
   }
}
javac client/MyApp.java
java client.MyApp
Advertisements

About SunKing2

Former web developer, guitar player, started Piano 2012 -yes still doing it! Recovery, sobriety, mental health advocate.
This entry was posted in java, web services. Bookmark the permalink.

2 Responses to Create a web service and client in 5 minutes with JAX-WS

  1. Anonymous says:

    Awesome

  2. Pingback: Understanding Java Web Services JAX-WS and WSDL | Connected Web

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s