JAX RS – 27 – Exception Mapping & Exception Hierarchy

Merhaba Arkadaslar,
Bu bolumde JAX-RS exception konusuna devam ediyoruz.

Exception Mapping

ExceptionMapper.java
javax.ws.rs.ext paketinde ExceptionMapper arabirimi yer almaktadir.

ExceptionMappers are custom, application provided, components that can catch 
thrown application exceptions and write specific HTTP responses. 
The are classes annotated with @Provider and that implement this interface
package javax.ws.rs.ext;

public interface ExceptionMapper<E extends Throwable> {

    Response toResponse(E exception);
}

ExceptionMapper implementation sinifinda @Provider annotation’ini kullanilmalidir.
Oncelikle yeni bir Exception sinifi tanimlayalim.

CustomerNotFoundException.java

package _20.exception.mapper.service;

public class CustomerNotFoundException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	public CustomerNotFoundException(String message) {
		super(message);
	}

}

CustomerResource.java
Kodumuzu inceleyelim , dikkat edecek olursak CustomerNotFoundException firlatiyoruz.

package _20.exception.mapper.service;

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

import _19.exception.model.Customer;
import _19.exception.service.CustomerService;

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

	@GET
	@Path("/customer/{id}")
	public Customer getCustomer(@PathParam("id") int id) {

		CustomerService service = new CustomerService();

		Customer customer = service.findCustomer(id);
		if (customer == null) {
			throw new CustomerNotFoundException("Could not find customer id : " + id);
		}
		return customer;

	}

}

NotFoundExceptionMapper.java
NotFoundExceptionMapper sinifimiz , ExceptionMapper arabirimini uygulayacak.
toResponse metodunu override edelim. Response objemizi ilgili sekilde ayarliyoruz ve geriye donuyoruz.

package _20.exception.mapper.service;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class NotFoundExceptionMapper implements ExceptionMapper<CustomerNotFoundException> {

	@Override
	public Response toResponse(CustomerNotFoundException exception) {
		ResponseBuilder builder = Response.status(Response.Status.NOT_FOUND);
		builder.entity(exception.getMessage());
		builder.type(MediaType.TEXT_PLAIN);
		return builder.build();
	}

}

Ornegimizi calistiralim ;

http://localhost:8080/injavawetrust.resteasy.tutorial/customer-exception-mapper/customer/1
http://localhost:8080/injavawetrust.jersey.tutorial/customer-exception-mapper/customer/1

404-mapper-not-found

Exception Hierarchy

JAX-RS 2.0’da , HTTP error durumlarina karsilik exception siniflari yer almaktadir. WebApplicationException objesi’ni ilgili status code’la olusturmak yerine bu tanimli siniflari kullanabiliriz.
Tabloyu inceleyecek olursak ilgili Status Code’lara karsilik siniflari gorebiliriz;

jaxrs-exception-hierarchy

CustomerResource.java

package _21.exception.hierarchy.service;

import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.NotSupportedException;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.ServiceUnavailableException;

import _19.exception.model.Customer;
import _19.exception.service.CustomerService;

@Path("/customer-exception-hierarchy")
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 NotFoundException();
		}
		return customer;

	}

	@GET
	@Path("/name/{id}")
	public String getCustomerName(@PathParam("id") int id) {
		System.out.println("getCustomerName is called...");
		//
		//
		throw new NotSupportedException();
	}

	@GET
	@Path("/address/{id}")
	public String getCustomerAddress(@PathParam("id") int id) {
		System.out.println("getCustomerAddress is called...");
		///
		///
		throw new ServiceUnavailableException();
	}

}

Ornegimizi calistirdigimizda ;

http://localhost:8080/injavawetrust.jersey.tutorial/customer-exception-hierarchy/customer/1
http://localhost:8080/injavawetrust.resteasy.tutorial/customer-exception-hierarchy/customer/1

http://localhost:8080/injavawetrust.jersey.tutorial/customer-exception-hierarchy/name/1
http://localhost:8080/injavawetrust.resteasy.tutorial/customer-exception-hierarchy/name/1

http://localhost:8080/injavawetrust.resteasy.tutorial/customer-exception-hierarchy/address/1
http://localhost:8080/injavawetrust.jersey.tutorial/customer-exception-hierarchy/address/1

Ornek olarak ;

http-415

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.