org.glassfish.jersey.examples.exception.Exceptions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of exception-mapping Show documentation
Show all versions of exception-mapping Show documentation
Jersey example showing exception mappers in action.
The newest version!
/*
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.glassfish.jersey.examples.exception;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
/**
* Exceptions class.
*
* @author Santiago.Pericas-Geertsen at oracle.com
*/
public class Exceptions {
// -- Exceptions
public static class MyException extends RuntimeException {
private Response response;
public MyException(Response response) {
this.response = response;
}
public Response getResponse() {
return response;
}
}
public static class MySubException extends MyException {
public MySubException(Response response) {
super(response);
}
}
public static class MySubSubException extends MySubException {
public MySubSubException(Response response) {
super(response);
}
}
// -- Exception Mappers
@Provider
public static class MyExceptionMapper implements ExceptionMapper {
@Override
public Response toResponse(MyException exception) {
Response r = exception.getResponse();
return Response.status(r.getStatus()).entity(
"Code:" + r.getStatus() + ":" + getClass().getSimpleName()).build();
}
}
@Provider
public static class MySubExceptionMapper implements ExceptionMapper {
@Override
public Response toResponse(MySubException exception) {
Response r = exception.getResponse();
return Response.status(r.getStatus()).entity(
"Code:" + r.getStatus() + ":" + getClass().getSimpleName()).build();
}
}
@Provider
public static class WebApplicationExceptionMapper implements ExceptionMapper {
@Override
public Response toResponse(WebApplicationException exception) {
Response r = exception.getResponse();
return Response.status(r.getStatus()).entity("Code:" + r.getStatus() + ":"
+ getClass().getSimpleName()).build();
}
}
}