JAX RS – 06 – Jersey & Apache Tomcat Implementation

Merhaba Arkadaslar
Bu bolumde Jersey & Apache Tomcat Implementation’i gerceklestirecegiz.

Jersey , lightweight http containerlarindan , Java EE Application Server’lara bir cok uygulama uzerinde calisabilir , kosabilir.

Jersey supports wide range of server environments from lightweight http containers 
up to full-fledged Java EE servers. 
// Jersey documentation

https://jersey.github.io/

Bu bolumde 3 farkli uygulama yapacagiz.

Dependency

Yeni bir proje olusturalim ve pom.xml dosyamiza Jersey icin dependency tanimini yapalim.

pom.xml

...
	<properties>
		<jersey.version>2.25.1</jersey.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.glassfish.jersey.containers</groupId>
			<!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
			<artifactId>jersey-container-servlet</artifactId>
			<version>${jersey.version}</version>
		</dependency>
	</dependencies>
...

Register with Web.xml (Deployment Descriptor)

web.xml
JSR 339 spect’inde 2.3.2 bolumunde su ifade gecmektedir ;

If no Application subclass is present, JAX-RS implementations are REQUIRED 
to dynamically add a servlet and set its name to
javax.ws.rs.core.Application

Eger Application sinifinin alt sinifi/subclass tanimli degilse , dinamik olarak javax.ws.rs.core.Application servlet tanimi olarak eklenmelidir.

Deployment Descriptor dosyamizda javax.ws.rs.core.Application icin su sekilde bir tanim yapiyoruz.

url-pattern olarak /application/* verdik.

...
	<servlet>
		<servlet-name>javax.ws.rs.core.Application</servlet-name>
	</servlet>
	<servlet-mapping>
		<servlet-name>javax.ws.rs.core.Application</servlet-name>
		<url-pattern>/application/*</url-pattern>
	</servlet-mapping>
...

MessageService.java

package controller;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/messages")
public class MessageService {

	@GET
	@Path("/greeting")
	// http://localhost:8080/injavawetrust.jersey/application/messages/greeting
	public String getMessage() {

		String message = "Welcome to injavawetrust.com Jersey Tutorial!";
		return message;

	}
}

Project Structure

injavawetrust.jersey

Run Application

http://localhost:8080/injavawetrust.jersey/application/messages/greeting

Example 2 / Uygulama 2

Bir baska yaklasim olarak RESTEasy orneklerimizde kullandigimiz gibi javax.ws.rs.core.Application sinifini extends ettigimiz register sinifini kullanabiliriz.
Bir baska proje olusturdum , bu projeye de Github uzerinde ulasabilirsiniz. (injavawetrust.jersey.register)

JAX-RS Spec’te su sekilde gecmektedir ;

If no servlet handles this application, JAX-RS implementations are REQUIRED to 
dynamically add a servlet whose fully qualified name must be 
that of the Application subclass.

Spect’e belirtildigi uzere Application sinifinin alt sinifini servlet tanimi olarak ekleyebiliriz.

web.xml

....
	<servlet>
		<servlet-name>service.RegisterApplication</servlet-name>
	</servlet>
	<servlet-mapping>
		<servlet-name>service.RegisterApplication</servlet-name>
		<url-pattern>/application-subclass/*</url-pattern>
	</servlet-mapping>
....

RegisterApplication.java

package service;

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;

import controller.MessageService;

public class RegisterApplication extends Application {

	private Set<Object> singletons = new HashSet<Object>();

	public RegisterApplication() {
		singletons.add(new MessageService());
	}

	@Override
	public Set<Object> getSingletons() {
		return singletons;
	}
}

MessageService.java

package controller;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/messages")
public class MessageService {

	@GET
	@Path("/greeting")
        //http://localhost:8080/injavawetrust.jersey.register/application-subclass/messages/greeting
	public String getMessage() {

		String message = "Welcome to injavawetrust.com Jersey Tutorial !";
		return message;

	}
}

Project Structure

injavawetrust.jersey.register


Run Application

http://localhost:8080/injavawetrust.jersey.register/application-subclass/messages/greeting

Example 3 / Uygulama 3

Bir baska yaklasim olarak @ApplicationPath annotation’ini kullanabiliriz.
Bu durumda web.xml dosyamiza gerek olmayacak.

 If the Application subclass is annotated with @ApplicationPath, 
 implementations are REQUIRED to use the value of this annotation appended
 with "/*" to define a mapping for the added server.
 Otherwise, the application MUST be packaged with a web.xml that specifies a servlet mapping.

Spect’e gore Application sinifinin subclass/alt sinifi eger @ApplicationPath annotation ile tanimlanmissa bu durumda mapping adresi /* ile bitmelidir.
Ya da web.xml de tanimlanacaksa (bir onceki ornegimizde oldugu gibi) servlet mappingde tanimlanmalidir.
Bu durum Jersey implementation’da web.xml icin gerekli iken @ApplicationPath annotation icin gerekli degildir. Burada spect’e uymuyor.

RegisterApplication.java

package service;


import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import controller.MessageService;

@ApplicationPath("/root-path")
public class RegisterApplication extends Application{

	   private Set<Object> singletons = new HashSet<Object>();
	   
	   public RegisterApplication() {
	      singletons.add(new MessageService());
	   }
	   
	   @Override
	   public Set<Object> getSingletons() {
	      return singletons;
	   }
}

MessageService.java

package controller;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/messages")
public class MessageService {

	@GET
	@Path("/greeting")
	// http://localhost:8080/injavawetrust.jersey.annotation/root-path/messages/greeting
	public String getMessage() {

		String message = "Welcome to injavawetrust.com Jersey Example with @ApplicationPath !";
		return message;

	}
}

Project Structure

injavawetrust.jersey.annotation

Run Application

 http://localhost:8080/injavawetrust.jersey.annotation/root-path/messages/message

Kaynak kodlar/ Source
injavawetrust.jersey

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 *