Creating a sample web service quickly

From time to time 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package nl.redstream.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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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.redstream.nl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.redstream.nl/" name="EchoServerService">
<types>
  <xsd:schema>
    <xsd:import namespace="http://ws.redstream.nl/" 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.

tags:

About Pascal Alma

Pascal started as an Oracle Developer in 1997 and developed numerous applications with Oracle Designer/Developer and PL/SQL. Since 2001 Pascal becomes more and more active with the development of software at the Java/J2EE platform. Nowadays Pascal is a senior JEE Developer/ Architect and has a lot of experience with several open source initiatives/ frameworks especially within the Enterprise Integration area. Besides these technical skills Pascal is a big Scrum enthusiastic.

Comments are closed.