Tutorial JAX-WS (SOAP)

JAX-WS sudah bundled dengan JDK versi 1.6, jadi kita tidak perlu lagi mendownload library javax.ws lagi.  Pada kesempatan kali ini saya akan menerangkan bagaimana cara membuat perhitungan dengan menggunakan Web Service. Tutorial ini terdiri dari beberapa langkah yang harus di ikuti :

  1. Create a SOAP-based RPC style Web Service Endpoint by using JAX-WS.
  2. Create a Java web service client manually.

untuk tutorial ini menggunakan style RPC web service

lets begin…

Web Service Endpoint…

1. Create a SOAP-based RPC style web service

package com.yudi.webservice;

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

@WebService
@SOAPBinding(style = Style.RPC)
public interface CalculatorInterface {

	@WebMethod int calculator(int param1, int param2, String operator);

}

2. Create a SOAP-based RPC style web service implementation

package com.yudi.impl;

import javax.jws.WebMethod;
import javax.jws.WebService;

import com.yudi.webservice.CalculatorInterface;

@WebService(endpointInterface = "com.yudi.webservice.CalculatorInterface")
public class CalculatorImpl implements CalculatorInterface{

@WebMethod
 public int calculator(int param1, int param2, String operator) {
 // TODO Auto-generated method stub
 int hasil =0;
    if("*".equals(operator)){
       hasil = param1 * param2;
    } else if("+".equals(operator)){
       hasil = param1 + param2;
    } else {
       hasil = param1 - param2;
    }
      return hasil;
    }
}

3. Create a Endpoint Publisher

package com.yudi.publish;

import javax.xml.ws.Endpoint;
import com.yudi.impl.CalculatorImpl;

public class CalculatorPublish {

public static void main(String[] args) {

  Endpoint.publish("http://localhost:9999/ws/calc", new CalculatorImpl());

}

}

jalankan endpoint publisher, dan “calculator web service” buatan anda telah ter-deployed di URL “http://localhost:9999/ws/calc

4. Test Web Service

Kita bisa bisa test web service yang telah kita deploy tadi dengan mengakses generate WSDL via URL “http://localhost:9999/ws/calc?wsdl


Web Service Client…

Ok, web service telah ter-deploy, sekarang mari kita lihat bagaimana cara untuk membuat web service client untuk mengakses web service ktia tadi yang telah di publish.

1. Java Web Service Client


package com.yudi.main;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

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

import com.yudi.ws.CalculatorInterface;

public class main {

public static void main(String[] args) {

try {
       URL url = new URL("http://localhost:9999/ws/calc?wsdl");
       QName qName = new QName("http://impl.yudi.com/", "CalculatorImplService");

       Service service = Service.create(url, qName);
       CalculatorInterface calc = service.getPort(CalculatorInterface.class);

       System.out.println("hasil calculasi = "+ calc.calculator(5, 5, "+"));
 } catch (MalformedURLException e) {
 // TODO Auto-generated catch block
       e.printStackTrace();
 }
 }
}

Output


hasil calculasi =10

Done… Silahkan di komentari…

Refference : Mkyong.com

Leave a comment