JAX RS – 08 – Plugins & @Path & Sub Resource Locator
Merhaba Arkadaslar
Bu bolumde @Path annotation’ini inceleyecegiz , oncesinde Firefox ve Chrome icin REST Client pluginlerinden birkac plugin linki verecegim.
Onceki bolumlerde JAX-RS implementation’larindan Jersey ve RESTEasy icin farkli konfigurasyonlar ile orneklerimizi uyguladik. Bundan sonraki bolumlerde hem Jersey hem de RESTEasy icin yeni bir proje olusturacagim ve bu proje uzerinden gidecegim. Bu projeleri ve onceki projeleri Github hesabim uzerinden bulabilirsiniz.
injavawetrust.jersey
injavawetrust.resteasy
Plugins for Chrome & Firefox
Oncelikle Chrome & Firefox icin pluginleri ekleyelim. Diledigizi ya da farkli bir plugin ekleyebilirsiniz.
Chrome icin;
Advanced REST client
RESTEasy Client
Insomnia REST Client
Firefox plugin;
REST Easy Client for Firefox
RESTClient
@Path
@Path annotation’ini onceki bolumlerde kullandik , simdi biraz daha detayli olarak incelemeye baslayalim.
@Path annotation’ini , JAX-RS tarafindan ilgili HTTP istegine karsilik gelen , URI bilgisini tanimlamak icin kullanilir.
The @javax.ws.rs.Path annotation in JAX-RS is used to define a URI matching pattern for incoming HTTP requests.
CustomerResource.java
package _01.pathAndPathParam.services; import javax.ws.rs.GET; import javax.ws.rs.Path; @Path("/customer-path") public class CustomerResource { @GET public String greeting() { String message = "greeting is called."; return message; } }
Root Resource icin HTTP request’i karsilayak metodu @Path annotation’ini kullanmadan tanimlayabiliriz.
Burada @GET annotation’ini kullandik , gelen ilgili HTTP GET istegini greeting metodu karsilayacaktir.
http://localhost:8080/injavawetrust.jersey.tutorial/customer-path http://localhost:8080/injavawetrust.resteasy.tutorial/customer-path
Ornegi hem Jersey hem de RESTEasy projelerimiz icin calistirdigimizda ;
Tabi ekledigimiz plugin’lerle test yapabiliriz.
Burada her bir Root Resource icin ilgili HTTP request(@GET) annotation’ina sahip SADECE bir tane metot tanimlanabilir. Bu durum Jersey de probleme neden olmaktadir , exception firlatir. RESTEasy Warning vermektedir.
Jersey icin ilgili durumda ;
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization. [[FATAL] A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by"@Consumes" and "@Produces" annotations at Java methods public java.lang.String _01.pathAndPathParam.service.CustomerResource.greeting() and public java.lang.String _01.pathAndPathParam.service.CustomerResource.greeting2() at matching regular expression /customer\-path. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.; source='org.glassfish.jersey.server.model.RuntimeResource@421b3437'] RESTEasy icin ilgili durumda ;
WARN: RESTEASY002142: Multiple resource methods match request "GET /customer-path". Selecting one. Matching methods: [public java.lang.String _01.pathAndPathParam.service.CustomerResource.greeting2(), public java.lang.String _01.pathAndPathParam.service.CustomerResource.greeting()]
@Path("/customer-path") public class CustomerResource { @GET public String greeting() { String message = "greeting is called."; return message; } @GET public String greeting2() { String message = "greeting2 is called."; return message; } }
Tabi ki diger HTTP request’lerine karsilik olarak da bir tane default metot sorunsuzca tanimlanabilir. (@POST , @PUT @DELETE vs icin. )
Ornek olarak @POST annotation da ekleyelim.
@POST public String greetingForPOST() { String message = "greetingForPOST is called."; return message; }
HTTP POST icin test edelim ;
Bir baska nokta olarak onceki orneklerde yaptigimiz gibi @Path annotation’ini metotlar ile de kullanabiliyoruz.
@GET @Path("/customers") public String getAllCustomers() { String message = "getAllCustomers is called."; return message; } @GET @Path("/vip.customers") public String getVIPCustomers() { String message = "getVIPCustomers is called."; return message; }
Ilgili metotlari test etmek icin ;
http://localhost:8080/injavawetrust.jersey.tutorial/customer-path/customers http://localhost:8080/injavawetrust.jersey.tutorial/customer-path/vip.customers http://localhost:8080/injavawetrust.resteasy.tutorial/customer-path/customers http://localhost:8080/injavawetrust.resteasy.tutorial/customer-path/vip.customers
Sub Resource Locator
Resource/root service siniflari bir istegi parcali olarak (partially) isleyebilir ve bir baska “sub” resource service sinifi istegi islemeye/karsilamaya devam edebilir.
Resource classes are able to partially process a request and provide another "sub" resource object that can process the remainder of the request
@Path annotation a sahip olursa ve herhangi bir HTTP method belirtilmemisse bu durumda sub-resource locator olarak degerlendirilecektir.
Subresource locators are Java methods annotated with @Path, but with no HTTP method annotation, like @GET, applied to them.
Resource methods that have a @Path annotation, but no HTTP method are considered sub- resource locators.
ShoppingStoreResource.java
Ornek kodumuzu inceleyecek olursak ShoppingStoreResource sinifimi root resource tur.
getProductResource metodu ise sub resource locator method tur.
package _01.path.service.subresource; import javax.ws.rs.Path; @Path("/shopping-path") public class ShoppingStoreResource { @Path("/product-subresources") public ProductResource getProductResource (){ System.out.println("ShoppingStoreResource#getProductResource is called."); ProductResource pr = new ProductResource(); return pr; } }
ProductResource.java
package _01.path.service.subresource; import javax.ws.rs.GET; public class ProductResource { @GET public String getProduct() { System.out.println("ProductResource#getProduct is called."); return "Macbook Pro"; } }
Ornegimizi calistirdigimizda;
http://localhost:8080/injavawetrust.resteasy.tutorial/shopping-path/product-subresources http://localhost:8080/injavawetrust.jerseyt.tutorial/shopping-path/product-subresources
Eclipse console da;
ShoppingStoreResource#getProductResource is called. ProductResource#getProduct is called.
Dikkat edecek olursak path bilgisi olarak getProductResource metoduna gitsek de ek olarak getProduct metodu da calismaktadir.
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
Leave a Reply