JAX RS – 05 – Jersey & Grizzly HTTP Server Implementation

Merhaba Arkadaslar
Bu bolumde , JAX-RS reference implementation olan Jersey ve Grizzly HTTP Server uzerindeki uygulamasini inceleyecegiz.

RESTful uygulamamizi basit olarak bir Java SE uygulamasi ile yapabiliriz. Bunun icin Grizzly Web Server’i kullanabiliriz.

Maven Dependency

Yeni bir proje olusturalim ve maven dependency tanimlarini ekleyelim.

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

<dependencies>

	<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server -->

	<dependency>
		<groupId>org.glassfish.jersey.containers</groupId>
		<artifactId>jersey-container-grizzly2-http</artifactId>
		<version>${jersey.version}</version>
	</dependency>

	<!-- https://mvnrepository.com/artifact/org.glassfish.grizzly/grizzly-http-server -->

	<dependency>
		<groupId>org.glassfish.grizzly</groupId>
		<artifactId>grizzly-http-server</artifactId>
		<version>${grizzly.version}</version>
	</dependency>

</dependencies>

Simple RESTful Web Service

Simdi de basit olarak bir RESTful servis tanimlayalim.
Tek bir metodumuz olacak ve geriye basitce bir String message donecek.

MessageService.java

package service;

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

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

	@GET
	@Path("/greeting")
        // http://localhost:8080/injavawetrust.jersey.grizzly/messages/greeting
	public String getGreetingMessage() {
		return "Welcome to injavawetrust.com RESTful Tutorial!";
	}

}

Web Service Publisher

WebServicePublisher.java
Simdi de Grizzly HTTP Server uzerinde , RESTful Web Service’imizi deploy edelim.

package publisher;

import java.net.URI;

import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;

import service.MessageService;

public class WebServicePublisher {

	private static final String BASE_URI = "http://localhost:8080/injavawetrust.jersey.grizzly/";

	public static void main(String[] args) {

		// register package or class
		// ResourceConfig rc = new ResourceConfig().packages("service");

		ResourceConfig rc = new ResourceConfig().registerClasses(MessageService.class);
		GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
		System.out.println("WebServicePublisher is running...");
		System.out.println("You can check WADL ...");
		System.out.println("http://localhost:8080/injavawetrust.jersey.grizzly/application.wadl");
	}
}

WebServicePublisher sinifimizi inceleyelim

  • ResourceConfig sinifi yardimi ile , JAX-RS service class’larimizi ya da ilgili package’ta yer alan class’lari register edebiliriz.
    registerClasses(…) , packages(…)
  • GrizzlyHttpServerFactory#createHttpServer metoduna java.net.URI ve ResourceConfig objelerimizi geciyoruz.
    Web Service’in publish edilecegi adresimiz ;
http://localhost:8080/injavawetrust.jersey.grizzly/

WADL (Web Application Description Language) bilgisine ulasmak icin;

http://localhost:8080/injavawetrust.jersey.grizzly/application.wadl

Run & Test Application

Simdi de Grizzly HTTP Server’i calistiralim ve RESTful servisimizi test edelim.

WebServicePublisher sinifimizi calistirdigimizda Eclipse Console da ;

INFO: [HttpServer] Started.
WebServicePublisher is running...
You can check WADL ...
http://localhost:8080/injavawetrust.jersey.grizzly/application.wadl

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/griz_jersey_intro/Grizzly-Jersey-Intro.html

Kaynak kodlar/ Source
injavawetrust.jersey.grizzly

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 *