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:

package net.pascalalma.ws;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.ws.Endpoint;

/**
 * @author Pascal Alma
 */

@WebService
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 http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --><definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.pascalalma.net/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.pascalalma.net/" name="EchoServerService">
<types>
  <xsd:schema>
    <xsd:import namespace="http://ws.pascalalma.net/" schemaLocation="http://localhost:8181/echo?xsd=1"></xsd:import>
  </xsd:schema>
</types>
<message name="getEcho">
  <part name="parameters" element="tns:getEcho"></part>
</message>
<message name="getEchoResponse">
  <part name="parameters" element="tns:getEchoResponse"></part>
</message>
<portType name="EchoServer">
  <operation name="getEcho">
    <input message="tns:getEcho"></input>
    <output message="tns:getEchoResponse"></output>
  </operation>
</portType>
<binding name="EchoServerPortBinding" type="tns:EchoServer">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
  <operation name="getEcho">
    <soap:operation soapAction=""></soap:operation>
    <input>
      <soap:body use="literal"></soap:body>
    </input>
    <output>
      <soap:body use="literal"></soap:body>
    </output>
  </operation>
</binding>
<service name="EchoServerService">
  <port name="EchoServerPort" binding="tns:EchoServerPortBinding">
    <soap:address location="http://localhost:8181/echo"></soap:address>
  </port>
</service>
</definitions>

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

Advertisement

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in Technical, Web Service and tagged . Bookmark the permalink.