JAX – WS – 05 – Simple Apache CXF Example

Merhaba Arkadaslar
Bu bolumde bir baska JAX-WS implementation olan Apache CXF
Web Service projesini basit olarak publish edip test edecegiz.
(CXF “Celtix” ve “XFire” birlesiminden olusmaktadir.)

https://en.wikipedia.org/wiki/Apache_CXF
http://cxf.apache.org/

Apache CXF hem JAX-WS hem de JAX-RS icin JSR implementation destegi saglamaktadir.

JSR Support

  • JAX-WS – Java API for XML-Based Web Services (JAX-WS) 2.0 – JSR-224
  • Web Services Metadata for the Java Platform – JSR-181
  • JAX-RS – The Java API for RESTful Web Services – JSR-311
  • SAAJ – SOAP with Attachments API for Java (SAAJ) – JSR-67

Simple Apache CXF Example

Yeni bir Java project olusturalim ve Maven projesini donusturelim.

Maven Dependencies

pom.xml
pom.xml dosyamiza gerekli dependency tanimlarini ekleyelim.

...
	<properties>
		<cxf.version>3.1.10</cxf.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>${cxf.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>${cxf.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>${cxf.version}</version>
		</dependency>

	</dependencies>
...

Service

Yeni bir interface ve implementation olacak sekilde bir class olusturalim.
Basit olarak geriye mesaj donsun.

HelloWorld.java

package service;

public interface HelloWorld {
	
	String sayHi(String username);

}

HelloWorldImpl.java

package service;

public class HelloWorldImpl implements HelloWorld {
	
	@Override
	public String sayHi(String username) {
		System.out.println("sayHi is called !");
		return "Hello, " + username;
	}
}

Web Service Publisher

Simdi de Apache CXF Web Service’ini publish edelim.

CXFWebServicePublisher.java

package publisher;

import org.apache.cxf.frontend.ServerFactoryBean;

import service.HelloWorld;
import service.HelloWorldImpl;

public class CXFWebServicePublisher {

	private static String URL = "http://localhost:9000/Hello";
	
	public static void main(String args[]) throws Exception {

		ServerFactoryBean serverFactory = new ServerFactoryBean();
		HelloWorldImpl helloworldImpl = new HelloWorldImpl();
		
		serverFactory.setServiceClass(HelloWorld.class);
		serverFactory.setAddress(URL);
		serverFactory.setServiceBean(helloworldImpl);
		serverFactory.create();

		System.out.println("CXF Web Service is publishing...");
	}
}

Yeni bir ServerFactoryBean objesi olusturalim.
serviceClass olarak kullanacagimiz interface icin HelloWorld.class literal bilgisini veriyoruz.
address olarak Web Service’i publish edecegimiz URL bilgisini veriyoruz.
serviceBean olarak Web Service implementation class tipinde referans degisken veriyoruz.
Sonrasinda create metodunu cagiralim.

Boylelikle Web Service publish edilecek.
Ornegi calistirdigimizda ;

CXF Web Service is publishing...

Web Service’i publish ettigimiz adrese gittigimizde ;

http://localhost:9000/Hello?wsdl

Client

Client ornegimizi yazalim ;

package client;

import org.apache.cxf.frontend.ClientProxyFactoryBean;

import service.HelloWorld;

public final class Client {

	private static String URL = "http://localhost:9000/Hello";

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

		ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
		factory.setAddress(URL);
		
		HelloWorld client = factory.create(HelloWorld.class);
		
		System.out.println("Invoke sayHi()....");
		System.out.println(client.sayHi("Levent"));

	}

}

ClientProxyFactoryBean objemizi olusturuyoruz.
address olarak biraz once Publish ettigimiz URL adresini veriyoruz.
create metoduna HelloWorld.class literal bilgisini veriyoruz.

Sonrasinda sayHi metodunu test edelim.

Ornegi calistirdigimizda;
Eclipse Console’da

Apr 02, 2017 2:46:45 PM org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
INFO: Creating Service {http://service/}HelloWorld from class service.HelloWorld
Invoke sayHi()....
Hello, Levent

Eclipse Console’da

sayHi is called !

Eclipse Console’larda farkli outputlar arasinda gecis icin ;

Github kaynak kodlar / source folder
injavawetrust-jaxws-tutorial

injavawetrust.jaxws.cxf

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 *