JAX – WS – 08 – Apache CXF Example & Tomcat

Merhaba Arkadaslar
Bu bolumde Apache CXF’in Tomcat uzerinde publish edilmesini inceleyecegiz.

Create Dynamic Web Project

Eclipse’te yeni bir Dynamic Web project olusturalim ve Maven projesine cevirelim.
Onceki bolumde kullandigimiz dependency tanimlarini ekleyebiliriz , burada Web Service’i Tomcat uzerinde publish edecegimiz icin jetty dependency tanimini eklememize gerek yok.

pom.xml

<properties>
	<cxf.version>3.2.0</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>

	<!-- We are going to publish on Tomcat , dont need this dependency -->
	<!-- 
	<dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-rt-transports-http-jetty</artifactId>
		<version>${cxf.version}</version>
	</dependency>
	 -->
</dependencies>

Web Service Implementation

Simdi de basit bir Web Service hizmeti hazirlayalim. Onceki bolumlerde kullandigimiz interface-class yapisini kullanabiliriz.

HelloWorld.java

package service;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HelloWorld {

	String sayHi(@WebParam(name = "username") String username);

}

HelloWorldImpl.java

package service;

import javax.jws.WebService;

@WebService(endpointInterface = "service.HelloWorld")
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 Web Service’i publish edelim. Bunun icin ;

  • CXFNonSpringServlet class’ini extends edip , loadBus metodunu override edebiliriz.
    http://cxf.apache.org/docs/servlet-transport.html
  • CXFNonSpringServlet bir Servlet’tir , bu nedenle @WebServlet annotation’ini ile URL bilgisi verebiliriz ya da web.xml dosyasinda url-pattern tanimi yapabiliriz.
    Burada onemli nokta /* ya da /sampleurl/* seklinde URL bilgisi tanimlamamiz gereklidir.
  • Sonrasinda javax.xml.EndPoint ya da org.apache.cxf.frontend.ServerFactoryBean yardimiyla Web Service’i publish edebiliriz.

CXFNonSpringServletPublisher.java

package publisher;

import javax.servlet.ServletConfig;
import javax.servlet.annotation.WebServlet;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.frontend.ServerFactoryBean;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;

import service.HelloWorldImpl;

@WebServlet("/cxf.webservice/*")
public class CXFNonSpringServletPublisher extends CXFNonSpringServlet {

	private static final long serialVersionUID = 1L;

	@Override
	public void loadBus(ServletConfig servletConfig) {

		System.out.println("loadBus called ...");

		super.loadBus(servletConfig);
		Bus bus = getBus();
		BusFactory.setDefaultBus(bus);
		Endpoint.publish("/hellows", new HelloWorldImpl());

		// You can also use the simple frontend API to do this
		ServerFactoryBean factory = new ServerFactoryBean();
		factory.setBus(bus);
		factory.setServiceClass(HelloWorldImpl.class);
		factory.setAddress("/hellows2");
		factory.create();
	}
}

Run Application

Ornegimiz calistiralim ve SOAP UI uzerinde test edelim.

http://localhost:8080/injavawetrust.jaxws.cxf.web/cxf.webservice/

SOAP UI programinda publish ettigimiz hellows ya da hellows2 icin test yapabiliriz.

http://localhost:8080/injavawetrust.jaxws.cxf.web/cxf.webservice/hellows?wsdl
http://localhost:8080/injavawetrust.jaxws.cxf.web/cxf.webservice/hellows2?wsdl

Github kaynak kodlar / source folder
injavawetrust-jaxws-tutorial

injavawetrust.jaxws.cxf.web

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 *