JAX RS – 09 – @PathParam

Merhaba Arkadaslar
Bu bolumde @PathParam annotation’ini inceleyecegiz. Onceki bolumde @Path annotation’ini incelemistik.

CustomerResource RESTful servisimiz icin dusunelim , customer id degerine gore geriye ilgili Customer bilgilerini donuyoruz.
Bu durumda istegi karsilayacak metodumuzu nasil yazabiliriz ? Bu noktada @PathParam annotation’ini bizim icin cozum saglayacaktir.

@PathParam is a parameter annotation which allows you 
to map variable URI path fragments into your method call.
//RestEasy reference guide

CustomerResource.java

package _02.pathParam.service;

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

@Path("/customer-pathParam")
public class CustomerResource {

	@GET
	@Path("/customer/{id}")
	public String getCustomerById(@PathParam("id") String customerId) {

		String message = "getCustomerById is called. Customer Id : " + customerId;
		return message;

	}
	
}

Ornegimizi calistiralim ;

http://localhost:8080/injavawetrust.jersey.tutorial/customer-pathParam/customer/100
http://localhost:8080/injavawetrust.resteasy.tutorial/customer-pathParam/customer/100

pathparam-example1

{} parantez arasinda bir Path parameter tanimladik , dikkat edecek olursak bu {} arasindaki isimle @PathParam ‘ daki isim ayni olmalidir.

Bu durum Jersey icin problem cikarmaz ,String parametre icin null degeri atanirken , RESTEasy icin org.jboss.resteasy.spi.InternalServerErrorException firlatilmaktadir.
@PathParam(“id”) yerine @PathParam(“idx”) yazip test edersek RESTEasy icin console’da ;

org.jboss.resteasy.spi.InternalServerErrorException: RESTEASY003990: Unknown @PathParam: idx for path: /customer-pathParam/customer/100
	at org.jboss.resteasy.core.PathParamInjector.inject(PathParamInjector.java:117)
	at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:91)
	at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:114)
...

pathparam-example2

Birden fazla Path Parameter kullanmak istersek ;

	@GET
	@Path("customer/{firstname}-{lastname}")
	public String getCustomerByName(@PathParam("firstname") String firstname, @PathParam("lastname") String lastname) {
		String message = "getCustomerByName is called. Welcome , " + firstname + " " + lastname;
		return message;
	}

Dikkat edecek olursak Path Parameter’lari ayirmak icin (-) karekteri kullaniyoruz. Dilersek baska bir karakter de kullanabiliriz.

http://localhost:8080/injavawetrust.resteasy.tutorial/customer-pathParam/customer/levent-erguder
http://localhost:8080/injavawetrust.jersey.tutorial/customer-pathParam/customer/levent-erguder

Bir baska ornek olarak araya / da koyabiliriz;

OrderResource.java

package _02.pathParam.service;

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

@Path("/order-pathParam")
public class OrderResource {

	@GET
	@Path("/order/{day}/{month}/{year}")
	public String getOrdersByHistory(@PathParam("day") int day, @PathParam("month") int month,
			@PathParam("year") int year) {

		String message = "getOrdersByHistory is called. ";
		String date = day + "." + month + "." + year;
		return message + " " + date;
	}

}

Ornegimizi test etmek icin ;

http://localhost:8080/injavawetrust.resteasy.tutorial/order-pathParam/order/20/09/2016
http://localhost:8080/injavawetrust.jersey.tutorial/order-pathParam/order/20/09/2016

Bir baska ornek olarak Regex de kullanabiliriz. Ornegin id degerinin sadece numerik oldugunu varsayacak olursak \\d+ olarak ifade yazabiliriz.

Ya da diger meta character’leri de kullanabiliriz. Tamamen bizim hayal gucumuze ve regex bilgimize kalmis bir durum.

Regex konusunda detayli bilgi icin ;
Pure Java – 65 Searching – Pattern & Matcher

	@GET
	@Path("/order/{id : \\d+}")
	public String getOrderById(@PathParam("id") int id) {
		String message = "getOrderById int is called. Order Id : " + id;
		return message;
	}

Ornegi test etmek icin Path Parameter olarak \\d+ ye uygun olarak 5000 degerini verelim.

http://localhost:8080/injavawetrust.jersey.tutorial/order-pathParam/order/5000
http://localhost:8080/injavawetrust.resteasy.tutorial/order-pathParam/order/5000

Path Parameter olarak \\d+ ye uygun deger vermezsek bu durumda ilgili metot cagrilmayacaktir.

Jersey icin HTTP Status 404 – Not Found donecektir.

http://localhost:8080/injavawetrust.jersey.tutorial/order-pathParam/order/invalidNumeric
http://localhost:8080/injavawetrust.resteasy.tutorial/order-pathParam/order/invalidNumeric

Bir baska ornek olarak ;

	@GET
	@Path("/order/{name : Order-\\d+\\w+}")
	public String getOrderByOrderName(@PathParam("name") String orderName) {
		String message = "getOrderByOrderName int is called. Order Name : " + orderName;
		return message;
	}

Ilgili metodu test etmek icin regex’e karsilik gelecek istegimiz ;

http://localhost:8080/injavawetrust.resteasy.tutorial/order-pathParam/order/Order-1000Abc
http://localhost:8080/injavawetrust.jersey.tutorial/order-pathParam/order/Order-1000Abc

Github kaynak kodlar / source folder
injavawetrust.resteasy
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 *