JAX – WS – 02 – Publishing with a Standalone Web Server

Merhaba Arkadaslar
Onceki bolumde JAX-WS kavramini incelemeye baslamistik ve ornek bir uygulama yapmistik.
Boylece ilk JAX-WS ornegini gerceklestirmis olduk. Web Service’imizi publish ederken javax.xml.ws.Endpoint abstract class’indan yaralanmistik.

Bu ornegimizde Apache Tomcat uzerinde JAX-WS ‘i deploy edecegiz.

Create a new Dynamic Web Project & Maven

Eclipse’te yeni bir Dynamic Web Project olusturalim. Sonrasinda projemizi Maven projesine cevirecegiz bunun icin projemize sag tiklayip Configure -> Convert to Maven project diyelim.

NOT 1 : Direkt maven projesi de olusturabiliriz. Ben calismalarimda bu sekilde olusturmayi tercih ediyorum.
NOT 2 : Maven projesi olmak zorunda degil normal Dynamic Web Project de olur. Tabi bu durumda ilgili jar dosyasini indirip WEB-INF/lib klasoru altina ekleyin ya da Deployment Assembly’yi ayarlayin.

pom.xml
pom.xml dosyamiza projemiz icin gerekli dependency tanimini ekleyelim.

<dependencies>
	<!-- https://mvnrepository.com/artifact/com.sun.xml.ws/jaxws-rt -->
	<dependency>
		<groupId>com.sun.xml.ws</groupId>
		<artifactId>jaxws-rt</artifactId>
		<version>2.2.10</version>
	</dependency>
</dependencies>

Model

Employee.java
Employee sinifimizda ilgili ilgili instance variable , constructor ve getter setter’lari tanimlayalim.

package model;

public class Employee {

	private int id;
	private String name;
	private String surname;
	private double salary;

	public Employee() {
		super();
	}

	public Employee(int id, String name, String surname, double salary) {
		super();
		this.id = id;
		this.name = name;
		this.surname = surname;
		this.salary = salary;
	}
        
        //getters and setters

Employee ekleme , id degerine gore ilgili Employee bilgisini donme ve tum Employee’leri listeleme icin metotlar yazacagiz. Metodumuzda donus tipi List<Employee> oldugunda soyle bir hata verecektir ;

java.util.List is an interface, and JAXB can't handle interfaces.

Bu durumda ya bir Wrapper kullanabiliriz ya da geriye Array donebiliriz.

EmployeeListWrapper.java

package model;

import java.util.List;

public class EmployeeListWrapper {

	private List<Employee> employees;

	public List<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}
}

Service

EmployeeService interface’ini inceleyecek olursak addEmployee metodu parametre olarak Employee tipinde bir degisken almaktadir.

getEmployee
metodu da int tipinde bir parametre almakta ve geriye Employee donmektedir.

getEmployeesWithWrapper metodu geriye EmployeeWrapper donmektedir. EmployeeWrapper sinifinda List<Employee> instance variable yer almaktadir.

getEmployeesWithArray metodu geriye Employee[] donmektedir.

EmployeeService.java

package service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import model.Employee;
import model.EmployeeListWrapper;

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

	@WebMethod
	public void addEmployee(@WebParam(name = "employee") Employee employee);

	@WebMethod
	public Employee getEmployee(@WebParam(name = "id") int id);

	@WebMethod
	public EmployeeListWrapper getEmployeesWithWrapper();

	@WebMethod
	public Employee[] getEmployeesWithArray();

}

EmployeeServiceImpl.java hebele

package client;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

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

import model.Employee;
import service.EmployeeService;

public class Client {

	private static String WEB_SERVICE_WSDL_URL = "http://localhost:8080/injavawetrust.jaxws.web/employeeServiceWS?wsdl";

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

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

		EmployeeService employeeService = service.getPort(EmployeeService.class);
		// add employees
		employeeService.addEmployee(new Employee(1, "Levent", "Erguder", 1000));
		employeeService.addEmployee(new Employee(2, "Burak", "Okumus", 30000));
		employeeService.addEmployee(new Employee(3, "Recep", "Bostanci", 2000));
		employeeService.addEmployee(new Employee(4, "Ozkan", "Ozbas", 10000));

		// getEmployee
		Employee employee = employeeService.getEmployee(2);
		System.out.println("getEmployee :");
		System.out.println(employee);
		System.out.println();

		// getEmployeesWithWrapper
		List<Employee> employees = employeeService.getEmployeesWithWrapper().getEmployees();
		System.out.println("getEmployeesWithWrapper :");
		employees.stream().forEach(System.out::println);
		System.out.println();

		// getEmployeesWithArray
		List<Employee> employees2 = Arrays.asList(employeeService.getEmployeesWithArray());
		System.out.println("getEmployeesWithArray :");
		employees2.stream().forEach(System.out::println);

	}
}

XML Configuration

Deployment Descriptor (web.xml) ve sun-jaxws.xml dosyalari XML konfigurasyonu icin gerekli olacaktir.

sun-jaxws.xml
Bu dosyamiz WEB-INF klasoru altinda olmalidir.
Bu dosya JAX-WS RI (Reference Implementation) Deployment Descriptor olarak da anilmaktadir.
Yeni bir endpoint ekliyoruz. implementation attribute olarak packageName.className olacak sekilde @WebService olarak kullandigimiz sinifi belirtiyoruz.
url-pattern attribute olarak ilgili Web Service’in publish edilecegi adres bilgisini veriyoruz.

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
	version="2.0">

	<endpoint name="EmployeeServiceImpl" implementation="service.EmployeeServiceImpl"
		url-pattern="/employeeServiceWS" />

</endpoints> 

web.xml
oncelikle WSServletContextListener icin <listener> tanimi yapmaktayiz. WSServletContextListener , sun-jaxws.xml dosyasini parse etmektedir.

public final class WSServletContextListener
    implements ServletContextAttributeListener, ServletContextListener {
...
    static final String JAXWS_RI_RUNTIME = "/WEB-INF/sun-jaxws.xml";
...
}

Listener’lar hakkinda daha detayli bilgi icin ;

http://www.injavawetrust.com/servlet-jsp-13-listener/

Sonrasinda yeni bir Servlet tanimi ekliyoruz ; WSServlet
<url-pattern> olarak sun-jaxws.xml dosyasinda tanimladigimiz url-pattern ile ayni bilgiyi veriyoruz.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>injavawetrust.jaxws.tutorial</display-name>

	<listener>
	     <listener-class>
	     com.sun.xml.ws.transport.http.servlet.WSServletContextListener
             </listener-class>
	</listener>

	<servlet>
	     <servlet-name>JAXWSServlet</servlet-name>
	     <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
	     <load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
	     <servlet-name>JAXWSServlet</servlet-name>
	     <url-pattern>/employeeServiceWS</url-pattern>
	</servlet-mapping>

</web-app>

Publish & Client 

Projemizi deploy edelim …

http://localhost:8080/injavawetrust.jaxws.web/employeeServiceWS

Client.java
Onceki bolumde yaptigimiz gibi benzer sekilde Client.java sinimifizi uygulayalim.

package client;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

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

import model.Employee;
import service.EmployeeService;

public class Client {

	private static String WEB_SERVICE_WSDL_URL = "http://localhost:8080/injavawetrust.jaxws.web/employeeServiceWS?wsdl";

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

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

		EmployeeService employeeService = service.getPort(EmployeeService.class);
		// add employees
		employeeService.addEmployee(new Employee(1, "Levent", "Erguder", 1000));
		employeeService.addEmployee(new Employee(2, "Burak", "Okumus", 10000));
		employeeService.addEmployee(new Employee(3, "Recep", "Bostanci", 10000));

		// getEmployee
		Employee employee = employeeService.getEmployee(2);
		System.out.println("getEmployee :");
		System.out.println(employee);
		System.out.println();

		// getEmployeesWithWrapper
		List<Employee> employees = employeeService.getEmployeesWithWrapper().getEmployees();
		System.out.println("getEmployeesWithWrapper :");
		employees.stream().forEach(System.out::println);
		System.out.println();

		// getEmployeesWithArray
		List<Employee> employees2 = Arrays.asList(employeeService.getEmployeesWithArray());
		System.out.println("getEmployeesWithArray :");
		employees2.stream().forEach(System.out::println);

	}
}

Ornegi calistirdigimizda ;
Eclipse Console'da

getEmployee :
Employee [id=2, name=Burak, surname=Okumus, salary=10000.0]

getEmployeesWithWrapper :
Employee [id=1, name=Levent, surname=Erguder, salary=1000.0]
Employee [id=2, name=Burak, surname=Okumus, salary=10000.0]
Employee [id=3, name=Recep, surname=Bostanci, salary=10000.0]
Employee [id=4, name=Ozkan, surname=Ozbas, salary=10000.0]

getEmployeesWithArray :
Employee [id=1, name=Levent, surname=Erguder, salary=1000.0]
Employee [id=2, name=Burak, surname=Okumus, salary=10000.0]
Employee [id=3, name=Recep, surname=Bostanci, salary=10000.0]
Employee [id=4, name=Ozkan, surname=Ozbas, salary=10000.0]

Eclipse Tomcat Console'da

EmployeeServiceImpl#addEmployee is invoked.
EmployeeServiceImpl#getEmployee is invoked.
EmployeeServiceImpl#addEmployee is invoked.
EmployeeServiceImpl#getEmployee is invoked.
EmployeeServiceImpl#addEmployee is invoked.
EmployeeServiceImpl#getEmployee is invoked.
EmployeeServiceImpl#addEmployee is invoked.
EmployeeServiceImpl#getEmployee is invoked.
EmployeeServiceImpl#getEmployee is invoked.
EmployeeServiceImpl#getEmployeesWithWrapper is invoked.
EmployeeServiceImpl#getEmployeesWithArray is invoked.

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 *