Version 0.1
Copyright © 2008 Lars Vogel
23.11.2008
Table of Contents
Java 1.6 introduced the possibility to create standard SOAP webservices with the standard JDK.
Create a Java project "de.vogella.webservice.java6.first.provider". Create a package with the same name and then the following class.
package de.vogella.webservice.java6.first.provider;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.ws.Endpoint;
@WebService
public class WiseQuoteServer {
@SOAPBinding(style = Style.RPC)
public String getQuote(String category) {
if (category.equals("fun")) {
return "5 is a sufficient approximation of infinity.";
}
if (category.equals("work")) {
return "Remember to enjoy life, even during difficult situatons.";
} else {
return "Becoming a master is relatively easily. Do something well and then continue to do it for the next 20 years";
}
}
public static void main(String[] args) {
WiseQuoteServer server = new WiseQuoteServer();
Endpoint endpoint = Endpoint.publish(
"http://localhost:9191/wisequotes", server);
}
}
If you start your main program the service should be up and running.
Create now the client code. Using the console switch to an empty directory and call the wsimport command line tool which is part of the JDK.
wsimport -keep http://localhost:9191/wisequotes?wsdl

This call will create an interface with the webservice methods and factory to use the webservice.
Create a new Java project "de.vogella.webservice.java6.first.consumer" create a package with the same name, copy the created java classes to your new project / package and adjust the path name.
Create a new Java class TestWS.java with the following coding. The coding demonstrates how to get the connection directly and how to get the connection with specifying the URL.
package de.vogella.webservice.java6.first.consumer;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
public class TestWS {
public static void main(String[] args) {
// Settting up the server connection
WiseQuoteServerService service = new WiseQuoteServerService();
WiseQuoteServer servicePort = service.getWiseQuoteServerPort();
// Calling the webservice
System.out.println(servicePort.getQuote("fun"));
System.out.println(servicePort.getQuote("work"));
// Alternatively if you want to specific the URL directly
try {
URL url = new URL("http://localhost:9191/wisequotes?wsdl");
WiseQuoteServerService serviceWithUrl = new WiseQuoteServerService(
url,
new QName(
"http://provider.first.java6.webservice.vogella.de/",
"WiseQuoteServerService"));
WiseQuoteServer servicePortWithUrl = serviceWithUrl
.getWiseQuoteServerPort();
System.out.println(servicePortWithUrl.getQuote("fun"));
System.out.println(servicePortWithUrl.getQuote("work"));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
Thank you for practicing with this tutorial.
I maintain this tutorial in my private time. If you like the information please help me by using flattr or donating or by
|