vogella.de

Follow me on twitter
About Lars Vogel
Flattr this

Webservices with Java 1.6 - Tutorial

Lars Vogel

Version 0.1

23.11.2008

Abstract

This article describes how to create a simple SOAP webservice with standard Java 1.6.


Table of Contents

1. Webservices with standard Java
1.1. Overview
1.2. Process
2. Example
2.1. Create the server Java project
2.2.
2.3. Using the Webservice
3. Thank you
4. Links and Literature
4.1. Performance

1. Webservices with standard Java

1.1. Overview

Java 1.6 introduced the possibility to create standard SOAP webservices with the standard JDK.

1.2. Process

To use standard Java for creating a webservice you

  • create a standard java class

  • use annotations to describe the class as a webservice

  • run a command line tool to create the WSDL

  • you run the class for example via a main program

2. Example

2.1. Create the server Java project

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.

Tip

Try to access http://localhost:9191/wisequotes?wsdl via a browser. You should see the WSDL of your server.

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.

Tip

the JDK/bin directory needs to be in your path to be able to call wsimport.

				
wsimport -keep http://localhost:9191/wisequotes?wsdl

			

This call will create an interface with the webservice methods and factory to use the webservice.

2.3. Using 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.

Tip

Specifying the URL is useful in case you what to use a TCP-monitor to trace the TCP data. In this case you would direct the client to the TCP monitor which would re-direct the TCP data to the server and vice versa.

				
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();
		}

	}
}

			

3. Thank you

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 recommending this tutorial to other people.

Flattr this

4. Links and Literature

4.1. Performance

Not listet yet