JAX RS – 07 – Jersey & ResourceConfig

Merhaba Arkadaslar
Onceki bolumde javax.ws.rs.ApplicationPath sinifini kalitmistik. Bu bolumde bir baska yaklasim olarak org.glassfish.jersey.server.ResourceConfig sinifini kalitarak projemizi ayaga kaldiracagiz.

Dependency

Yeni bir proje olusturalim ve pom.xml dosyamiza Jersey icin dependency tanimini yapalim.
Projeyi yazinin sonunda yer alan link uzerinde (injavawetrust.jersey.resourceconfig) projesi olarak bulabilirsiniz.

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>
...

Deployment Descriptor & ResourceConfig

web.xml dosyamiza onceki bolumde oldugu gibi tanimi ekleyelim.

web.xml

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

	<servlet>
		<servlet-name>service.RegisterApplication</servlet-name>
	</servlet>
	<servlet-mapping>
		<servlet-name>service.RegisterApplication</servlet-name>
		<url-pattern>/root-path/*</url-pattern>
	</servlet-mapping>


</web-app>

RegisterApplication.java
Dikkat edecek olursak javax.ws.rs.ApplicationPath sinifi yerine org.glassfish.jersey.server.ResourceConfig sinifini kalitiyoruz.

package service;

import org.glassfish.jersey.server.ResourceConfig;

import controller.MessageService;

public class RegisterApplication extends ResourceConfig {

	public RegisterApplication() {
		registerClasses(MessageService.class);
	}

}

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.resourceconfig/root-path/messages/message
	public String getMessage() {

		String message = "Jersey + ResourceConfig register example!";
		return message;

	}
}

Run Application

Ornegimizi calistirip ilgili URI adresini test edebiliriz ;

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

@ApplicationPath Annotation

Bir baska ornek olarak @ApplicationPath annotation kullanabiliriz.
Yukaridaki ornegimize benzer sekilde olacak burada web.xml dosyamiz yerine @ApplicationPath annotation kullanacagiz.

Ilgili ornegi Github uzerinden indirebilirsiniz.
injavawetrust.jersey.resourceconfig.annotation

package service;

import javax.ws.rs.ApplicationPath;

import org.glassfish.jersey.server.ResourceConfig;

import controller.MessageService;

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

	public RegisterApplication() {
		registerClasses(MessageService.class);
	}

}

Run Application

Yeni projemizi calistirdigimizda;

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

 

Kaynak kodlar/ Source
injavawetrust.jersey.resourceconfig
injavawetrust.jersey.resourceconfig.annotation
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 *