Creating a sample web service quickly

Every now and then I am evaluating a tool or framework for which I need a web service. Although there are examples of running serviuces available on the web, I don´t always have access to the web and sometimes you need some more control over the service so you can edit/influence the corresponding WSDL. For these cases I created the following class which is all you need to get a web service running on your local machine (assuming you have JDK1.6 installed).
All the source you need is the following:


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

 * @author Pascal Alma

public class EchoServer {

	@SOAPBinding(style = Style.DOCUMENT)
	public String getEcho(String txt) {
			return "echo: " + txt;

	public static void main(String[] args) {
		EchoServer myServer = new EchoServer ();
		Endpoint endpoint = Endpoint.publish(
				"http://localhost:8181/echo", myServer);

When you run this class (and leave it running), you can point your browser to go to ‘http://localhost:8181/echo?wsdl’ and you receive the wsdl for the web service:

<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at RI's version is JAX-WS RI 2.1.6 in JDK 6. --><!-- Generated by JAX-WS RI at RI's version is JAX-WS RI 2.1.6 in JDK 6. --><definitions xmlns:soap="" xmlns:tns="" xmlns:xsd="" xmlns="" targetNamespace="" name="EchoServerService">
    <xsd:import namespace="" schemaLocation="http://localhost:8181/echo?xsd=1"></xsd:import>
<message name="getEcho">
  <part name="parameters" element="tns:getEcho"></part>
<message name="getEchoResponse">
  <part name="parameters" element="tns:getEchoResponse"></part>
<portType name="EchoServer">
  <operation name="getEcho">
    <input message="tns:getEcho"></input>
    <output message="tns:getEchoResponse"></output>
<binding name="EchoServerPortBinding" type="tns:EchoServer">
  <soap:binding transport="" style="document"></soap:binding>
  <operation name="getEcho">
    <soap:operation soapAction=""></soap:operation>
      <soap:body use="literal"></soap:body>
      <soap:body use="literal"></soap:body>
<service name="EchoServerService">
  <port name="EchoServerPort" binding="tns:EchoServerPortBinding">
    <soap:address location="http://localhost:8181/echo"></soap:address>

You can now test your tool/ product by using this web service.

About Pascal Alma

Pascal is a senior software developer and architect. Pascal has been designing and building applications since 2001. He is particularly interested in Open Source toolstack (Mule, Spring Framework, JBoss) and technologies like Web Services, SOA and Cloud technologies. Lately he is having great fun by building iOS apps with Swift. Specialties: JEE AWS XML/XSD/XSLT Web Services/SOA Mule ESB/ WSO2 ESB Maven Cloud Technology Swift/ iOS
This entry was posted in Technical, Web Service and tagged . Bookmark the permalink.