soap Getting started with soap Creating a Simple Web Service and Clients with JAX-WS (Document / literal)


Example

This is project directory.

Project Directory

  1. A service endpoint interface

First we will create a service endpoint interface. The javax.jws.WebService @WebService annotation defines the class as a web service endpoint.


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

// Service Interface with customize targetNamespace
@WebService(targetNamespace = "http://hello-soap/ws")
@SOAPBinding(style = Style.DOCUMENT, use=Use.LITERAL) //optional
public interface HelloSoap {

    @WebMethod String getHelloSoap(String name);

}

  1. Service endpoint implementation (SEI)

Next we will create service endpoint implementation. We will create an explicit interface by adding the endpointInterface element to the @WebService annotation in the implementation class. Here are some set of rules 28.1.1 Requirements of a JAX-WS Endpoint that JAX-WS endpoints must follow. The getHelloSoap method returns a greeting to the client with the name passed to it.


import javax.jws.WebService;

// Customized Service Implementation (portName,serviceName,targetNamespace are optional)

@WebService(portName = "HelloSoapPort", serviceName = "HelloSoapService",
endpointInterface = "com.wonderland.hellosoap.HelloSoap", targetNamespace = "http://hello-soap/ws")
public class HelloSoapImpl implements HelloSoap {

    @Override
    public String getHelloSoap(String name) {
        return "[JAX-WS] Hello : " + name;
    }

}

  1. Web service endpoint publisher

import javax.xml.ws.Endpoint;

public class HelloSoapPublisher {

    public static void main(String[] args) {
        // creating web service endpoint publisher
        Endpoint.publish("http://localhost:9000/ws/hello-soap", new HelloSoapImpl());
    }

}

  1. Next steps, we will run HelloSoapPublisher.java as java application. Then we will view the WSDL file by requesting the URL http://localhost:9000/ws/hello-soap?wsdl in a web browser.
http://localhost:9000/ws/hello-soap?wsdl

If XML data format is display at the web browser, then we are ready to go next step.

hello-soap?wsdl

Note:
If you get some kind of error message, maybe you need to use wsgen tool to generate necessary JAX-WS portable artifacts. We are not covered about wsgen tool here.

  1. Web Service Client

Final step, we will create a client that accesses our published service.


import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class HelloSoapClient {

    public static void main(String[] args) throws Exception {

        // create wsdl url
        URL wsdlDocumentUrl = new URL("http://localhost:8000/ws/hello-soap?wsdl");
        QName helloSoapService = new QName("http://hello-soap/ws", "HelloSoapService");
        // create web service
        Service service = Service.create(wsdlDocumentUrl, helloSoapService);
        // get object of pointed service port
        HelloSoap helloSoap = service.getPort(HelloSoap.class);
        // testing request
        System.out.println(helloSoap.getHelloSoap("Soap "));

    }

}

Output: [JAX-WS] Hello : Soap

Note: Port number changed to 8000 in our web service client. The reason here is, I used Eclipse IDE, build-in TCP/IP monitor tool to trace messages (More Info: How to trace SOAP message in Eclipse IDE). For functional testing purpose try SoapUI | Functional Testing for SOAP and REST APIs.