JAX RS – 26 – javax.ws.rs.WebApplicationException
Merhaba Arkadaslar
Bu bolumde javax.ws.rs.WebApplicationException sinifini inceleyecegiz.
Error’lar , kullaniciya/client’a uygun Response mesajiyla donulebilir ya da bir exception olarak firlatilabilir (throw exception)
Eger Exception Mapper tanimlandiysa firlatilan exception’lar , runtime/calisma zamaninda JAX-RS tarafindan handle edilir.
Exception Mapper , exception’i HTTP response’a donusturebilir/convert.
Eger firlatilan exception mapper tarafindan handle edilmiyorsa bu durumda Container tarafindan handle edilir.
javax.ws.rs.WebApplicationException
JAX-RS , javax.ws.rs.WebApplicationException sinifini saglar.
WebApplicationException , uygulama kodu tarafindan firlatildiginda JAX-RS bu exception’i yakalar ve getResponse() metodunu cagirir.
Eger status code ya da Response objesi kullaniliyorsa bunlar yardimiyla HTP response olusturulur , eger yoksa status code varsayilan olarak 500 olacaktir. “Internal Server Error” , client’a donecektir.
JAX-RS also provides the javax.ws.rs.WebApplicationException. This can be thrown by application code and automatically processed by JAX-RS without having to write an explicit mapper.
javax.ws.rs.WebApplicationException
public class WebApplicationException extends RuntimeException { public WebApplicationException() {...} public WebApplicationException(Response response) {...} public WebApplicationException(int status) {...} public WebApplicationException(Response.Status status) {...} public WebApplicationException(Throwable cause) {...} public WebApplicationException(Throwable cause, Response response) {...} public WebApplicationException(Throwable cause, int status) {...} public WebApplicationException(Throwable cause, Response.Status status) {...} public Response getResponse() {...] }
CustomerResource.java
Kodu inceleyelim…
Ilgili id degerinde Customer bulunmadiginda yeni bir WebApplicationException firlatiyoruz.
Response.Status.NOT_FOUND degerini arguman olarak veriyoruz.
Test amacli findCustomer metodu geriye null donmekte.
getCustomerName metodu da benzer sekilde WebApplicationException firlatiyor bura da Response.Status.NOT_IMPLEMENTED degerini veriyoruz.
package _19.exception.service; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; import _19.exception.model.Customer; @Path("/customer-exception") public class CustomerResource { @GET @Path("/customer/{id}") public Customer getCustomer(@PathParam("id") int id) { System.out.println("getCustomer is called...."); CustomerService service = new CustomerService(); Customer customer = service.findCustomer(id); if (customer == null) { throw new WebApplicationException(Response.Status.NOT_FOUND); } return customer; } @GET @Path("/name/{id}") public String getCustomerName(@PathParam("id") int id) { System.out.println("getCustomerName is called..."); throw new WebApplicationException(Response.Status.NOT_IMPLEMENTED); } }
CustomerService.java
package _19.exception.service; import _19.exception.model.Customer; public class CustomerService { public Customer findCustomer(int id) { // simply return null return null; } }
Ornegimizi test edelim ;
http://localhost:8080/injavawetrust.resteasy.tutorial/customer-exception/customer/1 http://localhost:8080/injavawetrust.jersey.tutorial/customer-exception/customer/1
RESTEasy icin ;
getCustomer is called.... ..... ERROR: RESTEASY002010: Failed to execute javax.ws.rs.WebApplicationException: HTTP 404 Not Found at _19.exception.service.CustomerResource.getCustomer(CustomerResource.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) ...
Jersey icin ;
Ornegimizi diger metot icin yapalim;
http://localhost:8080/injavawetrust.resteasy.tutorial/customer-exception/name/1 http://localhost:8080/injavawetrust.jersey.tutorial/customer-exception/name/1
RESTEasy icin ;
getCustomerName is called... ... ERROR: RESTEASY002010: Failed to execute javax.ws.rs.WebApplicationException: HTTP 501 Not Implemented at _19.exception.service.CustomerResource.getCustomerName(CustomerResource.java:33) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ....
Jersey icin ;
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