Friday, June 28, 2013

Creating a simple webservice using JAX-WS

This example creates a simple webservice using JAX-WS



Message.java 

package edu.demo.ws;

importjavax.jws.WebMethod;
importjavax.jws.WebService;

@WebService
public class Message {

      @WebMethod
      public String getMessage(String text) {
            return "Message " + text;
      }
}

Main.java


package edu.demo.ws;

importjavax.xml.ws.Endpoint;

public class Main {

      public static void main(String[] args) {
            Endpoint.publish("http://localhost:8181/message", new Message());

      }

}

Run the Main.java as Java Application and in the browser open the url  http://localhost:8181/message?wsdl



<?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.demo.edu/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.demo.edu/" name="MessageService">
- <types>
- <xsd:schema>
 <xsd:import namespace="http://ws.demo.edu/" schemaLocation="http://localhost:8181/message?xsd=1" />
 </xsd:schema>
 </types>
- <message name="getMessage">
 <part name="parameters" element="tns:getMessage" />
 </message>
- <message name="getMessageResponse">
 <part name="parameters" element="tns:getMessageResponse" />
 </message>
- <portType name="Message">
- <operation name="getMessage">
 <input message="tns:getMessage" />
 <output message="tns:getMessageResponse" />
 </operation>
 </portType>
- <binding name="MessagePortBinding" type="tns:Message">
 <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <operation name="getMessage">
 <soap:operation soapAction="" />
- <input>
 <soap:body use="literal" />
 </input>
- <output>
 <soap:body use="literal" />
 </output>
 </operation>
 </binding>
- <service name="MessageService">
- <port name="MessagePort" binding="tns:MessagePortBinding">
 <soap:address location="http://localhost:8181/message" />
 </port>
 </service>
 </definitions>

  Use the URL in the above wsdl under SchemaLocation http://localhost:8181/message?xsd=1



 <?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.
 -->
- <xs:schema xmlns:tns="http://ws.demo.edu/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://ws.demo.edu/">
 <xs:element name="getMessage" type="tns:getMessage" />
 <xs:element name="getMessageResponse" type="tns:getMessageResponse" />
- <xs:complexType name="getMessage">
- <xs:sequence>
 <xs:element name="arg0" type="xs:string" minOccurs="0" />
 </xs:sequence>
 </xs:complexType>
- <xs:complexType name="getMessageResponse">
- <xs:sequence>
 <xs:element name="return" type="xs:string" minOccurs="0" />
 </xs:sequence>
 </xs:complexType>
 </xs:schema>


Use SoapUI or Webservices Explorer in eclipse, post the Wsdl link and you should be able to use your first webservice