JAX – WS – 02 – Simple JAX – WS Implementation

Merhaba Arkadaslar
Onceki bolumde JAX-WS , SOAP , WSDL gibi kavramlari inceledik.
Bu bolumde basit bir JAX-WS uygulamasi yapacagiz.

Simple JAX-WS Implementation

Simdi de ornek bir uygulama gerceklestirelim. Ornegimiz Java SE temelli olacak.

MessageWriter.java
Yeni bir interface olusturalim.
@WebService annotation’i , @WebServlet tanimi gibi JAX-WS icin deklarasyon yapiyoruz.

The @WebService annotation defines the class as a web service endpoint.

@SOAPBinding ile RPC style deklarasyon tanimi ekliyoruz.
Varsayila olarak Style.DOCUMENT tir.

WebService methodlari @WebMethod annotation’i kullaniyoruz.
@WebParam annotation’i ile WSDL dokumanindaki parametre ismini duzenliyoruz.
Varsayilan olarak arg0 , arg1 … dir.

Bu interface’e SEI (Service Endpoint Interface) adi verilmektedir. Bu interface’in implementation’ina ise SIB (Service Implementation Bean) adi verilmektedir.

JAX-WS end point tanimlamak icin  interface tanimlamak zorunlu degildir. Bununla birlikte hem SEI hem SIB tanimlamasini yapalim.

package injavawetrust.ws.service;

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

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

	@WebMethod
	public String getWelcomeMessage(@WebParam(name = "name") String name, 
					@WebParam(name = "surname") String surname);
}

MessageWriterImpl.java
MessageWriterImpl class’i MessageWriter interface’ini implements etmektedir.
@WebService annotation’da endpointInterface attribute’te full name olarak veriyoruz.
(packageName.className)
Basit olarak metodumuz geriye bir String message donmektedir.

package injavawetrust.ws.service;

import javax.jws.WebService;

@WebService(endpointInterface = "injavawetrust.ws.service.MessageWriter")
public class MessageWriterImpl implements MessageWriter {

	@Override
	public String getWelcomeMessage(String name, String surname) {
		return "Welcome , " + name + " " + surname + ". This message is sent by MessageWriterImpl#getWelcomeMessage.";
	}

}

WebServicePublisher.java
Simdi de Publisher sinifimizi yazalim.
Web Service’imizi yayinlayacagimiz/publish URL adresini belirtelim.

javax.xml.ws.Endpoint publish metoduna ilgili Web Service URL bilgisini ve Web Service implementation olarak olusturdugumuz MessageWriterImpl objesini veriyoruz.

package injavawetrust.ws.publisher;

import javax.xml.ws.Endpoint;

import injavawetrust.ws.service.MessageWriterImpl;

public class WebServicePublisher {

	private static String WEB_SERVICE_URL = "http://localhost:8888/injavawetrust/hello.jaxws";

	public static void main(String[] args) {
		Endpoint.publish(WEB_SERVICE_URL, new MessageWriterImpl());
		System.out.println("Web Service is started.");
	}

}

Ornegimizi calistirdigimizda basarili sekilde 8888 portunda Web Service publish edilecektir.

Web Service is started.

Web Service’in publish edildigi URL’e gidelim ve wsdl icerigini gorelim.
Bunun icin ?wsdl parametresi ekleyelim

http://localhost:8888/injavawetrust/hello.jaxws?wsdl
<!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS 
	RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. -->
<!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS 
	RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. -->
<definitions
	xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
	xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"
	xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://service.ws.injavawetrust/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.ws.injavawetrust/"
	name="MessageWriterImplService">
	<types />
	<message name="getWelcomeMessage">
		<part name="name" type="xsd:string" />
		<part name="surname" type="xsd:string" />
	</message>
	<message name="getWelcomeMessageResponse">
		<part name="return" type="xsd:string" />
	</message>
	<portType name="MessageWriter">
		<operation name="getWelcomeMessage" parameterOrder="name surname">
			<input
				wsam:Action="http://service.ws.injavawetrust/MessageWriter/getWelcomeMessageRequest"
				message="tns:getWelcomeMessage" />
			<output
				wsam:Action="http://service.ws.injavawetrust/MessageWriter/getWelcomeMessageResponse"
				message="tns:getWelcomeMessageResponse" />
		</operation>
	</portType>
	<binding name="MessageWriterImplPortBinding" type="tns:MessageWriter">
		<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
			style="rpc" />
		<operation name="getWelcomeMessage">
			<soap:operation soapAction="" />
			<input>
				<soap:body use="literal" namespace="http://service.ws.injavawetrust/" />
			</input>
			<output>
				<soap:body use="literal" namespace="http://service.ws.injavawetrust/" />
			</output>
		</operation>
	</binding>
	<service name="MessageWriterImplService">
		<port name="MessageWriterImplPort" binding="tns:MessageWriterImplPortBinding">
			<soap:address location="http://localhost:8888/injavawetrust/hello.jaxws" />
		</port>
	</service>
</definitions>

Client.java
Simdi de Client ornegimizi yazalim. yeni bir java.net.URL objesini olusturuyoruz ve Web Service’i publish ettigimiz URL bilgisini veriyoruz.

QName objesini olusturuyoruz targetSpace ve name bilgisini veriyoruz.
Sonrasinda javax.xml.ws.Service#create metoduna url ve qname bilgisini veriyoruz.
Burada dikkat edecek olursak SIB (Service Implementation Bean) isminin sonuna Service eki geldi.

MessageWriterImplService

getPort metoduna class literal bilgisini veriyoruz , sonrasinda getWelcomeMessage i invoke ediyoruz.

package injavawetrust.ws.client;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import injavawetrust.ws.service.MessageWriter;

//1- run the WebServicePublisher
//2- test Client.java

public class Client {
	private static String WEB_SERVICE_WSDL_URL = "http://localhost:8888/injavawetrust/hello.jaxws?wsdl";

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

		URL url = new URL(WEB_SERVICE_WSDL_URL);
		QName qname = new QName("http://service.ws.injavawetrust/", "MessageWriterImplService");
		Service service = Service.create(url, qname);

		MessageWriter messageWriter = service.getPort(MessageWriter.class);

		String welcomeMessage = messageWriter.getWelcomeMessage("Levent", "Erguder");
		System.out.println(welcomeMessage);
	}
}

Ornegimizi calistirdigimizda ;

Welcome , Levent Erguder. This message is sent by MessageWriterImpl#getWelcomeMessage.

Github kaynak kodlar / source folder
injavawetrust-jaxws-tutorial

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *