JAX RS – 03 – RESTEasy & Apache Tomcat Implementation with @ApplicationPath

Merhaba Arkadaslar ,
Onceki bolumde RESTEasy uygulamasini Apache Tomcat uzerinde ayaga kaldirdik. Konfigurasyonlarimizi XML araciligiyla gerceklestirdik. Bu bolumde @ApplicationPath annotation’ini kullanacagiz ve boylece XML konfigurasyonu yapmamiza gerek kalmayacak.

RestEasy reference guide :
if the application jar contains an Application class (or a subclass thereof) which is 
annotated with an ApplicationPath annotation, a web.xml file isn't even needed.

If there is an Application class but it doesn't have an @ApplicationPath annotation, 
then a web.xml file with at least a  element is required.

Konfigurasyonlarin karismamasi icin yeni bir proje olusturuyorum. Bu projeyi de Github hesabim uzerinde bulabilirsiniz.
(injavawetrust.resteasy.annotation)

Onceki bolumde projemize RESTEasy icin dependency eklemistik , bu bolumde yeni bir dependency ekleyecegiz. ; resteasy-servlet-initializer
Ek olarak Resteasy javax.servlet.ServletContainerInitializer interface’sini kullanarak ilgili entegrasyonu saglamaktadir.

Resteasy uses the ServletContainerInitializer integration interface in Servlet 3.0 
containers to initialize an application, automatically scanning for resources 
and providers. To enable automatic scanning, you must also 
include the resteasy-servlet-initializer
//Resteasy reference guide

pom.xml

...
    <properties>
        <resteasy.version>3.6.2.Final</resteasy.version>
    </properties>

    <dependencies>
    
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>${resteasy.version}</version>
        </dependency>

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-servlet-initializer</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
        
    </dependencies>
...

RestMessageController.java
Onceki bolumde kullandigimiz class’i aynen kullaniyoruz.

package controller;

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

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

	@GET
	@Path("/message")
	public String getMessage() {

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

	}
}

RegisterApplication.java
Onceki bolumdeki koddan farkli olarak @ApplicationPath annotation’ini kullaniyoruz.
@ApplicationPath annotation’ini JAX-RS service siniflari icin base/kok/taban URL path bilgisini tanimlar.

The javax.ws.rs.core.Application class is a standard JAX-RS class 
that you may implement to provide information on your deployment
The @ApplicationPath defines the relative base URL path for all our 
JAX-RS services in the deployment
package service;


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

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

import controller.RestMessageController;

@ApplicationPath("/root-path")

//@ApplicationPath("/") OK
//@ApplicationPath("/*") Don't use /*
//@ApplicationPath("/root-path/*") Don't use /*

public class RegisterApplication extends Application{

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

Project Structure’i inceleyecek olursak web.xml dosyamiz yer almayacaktir ;

project structure

Run Application

Ornegimizi calistiralim ;

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

run resteasy aplicationpath

Kaynak kodlar/Source :
injavawetrust.resteasy

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 *