Jersey(1.19.1) - WebApplicationException and Mapping Exceptions to Responses
Previous sections have shown how to return HTTP responses and it is possible to return HTTP errors using the same mechanism. However, sometimes when programming in Java it is more natural to use exceptions for HTTP errors.
The following example shows the throwing of a NotFoundException
from the bookmark sample:
@Path("items/{itemid}/")
public Item getItem(@PathParam("itemid") String itemid) {
Item i = getItems().get(itemid);
if (i == null) {
throw new NotFoundException("Item, " + itemid + ", is not found");
}
return i;
}
This exception is a Jersey specific exception that extends WebApplicationException and builds a HTTP response with the 404 status code and an optional message as the body of the response:
public class NotFoundException extends WebApplicationException {
/**
* Create a HTTP 404 (Not Found) exception.
*/
public NotFoundException() {
super(Responses.notFound().build());
} /**
* Create a HTTP 404 (Not Found) exception.
*
* @param message
* the String that is the entity of the 404 response.
*/
public NotFoundException(String message) {
super(Response.status(Responses.NOT_FOUND).entity(message).type("text/plain").build());
}
}
In other cases it may not be appropriate to throw instances of WebApplicationException, or classes that extend WebApplicationException, and instead it may be preferable to map an existing exception to a response. For such cases it is possible to use the ExceptionMapper<E extends Throwable> interface. For example, the following maps the EntityNotFoundException to a HTTP 404 (Not Found) response:
@Provider
public class EntityNotFoundMapper implements ExceptionMapper<javax.persistence.EntityNotFoundException> {
public Response toResponse(javax.persistence.EntityNotFoundException ex) {
return Response.status(404)
.entity(ex.getMessage())
.type("text/plain")
.build();
}
}
The above class is annotated with @Provider, this declares that the class is of interest to the JAX-RS runtime. Such a class may be added to the set of classes of the Application instance that is configured. When an application throws an EntityNotFoundException the toResponse
method of the EntityNotFoundMapper
instance will be invoked.
Jersey(1.19.1) - WebApplicationException and Mapping Exceptions to Responses的更多相关文章
- Jersey(1.19.1) - Hello World, Get started with a Web application
1. Maven Dependency <properties> <jersey.version>1.19.1</jersey.version> </prop ...
- Jersey(1.19.1) - JSON Support
Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T&g ...
- Jersey(1.19.1) - Hello World, Get started with Jersey using the embedded Grizzly server
Maven Dependencies The following Maven dependencies need to be added to the pom: <dependency> ...
- Jersey(1.19.1) - Root Resource Classes
Root resource classes are POJOs (Plain Old Java Objects) that are annotated with @Path have at least ...
- Jersey(1.19.1) - Deploying a RESTful Web Service
JAX-RS provides a deployment agnostic abstract class Application for declaring root resource and pro ...
- Jersey(1.19.1) - Extracting Request Parameters
Parameters of a resource method may be annotated with parameter-based annotations to extract informa ...
- Jersey(1.19.1) - Representations and Java Types
Previous sections on @Produces and @Consumes referred to MIME media types of representations and sho ...
- Jersey(1.19.1) - Conditional GETs and Returning 304 (Not Modified) Responses
Conditional GETs are a great way to reduce bandwidth, and potentially server-side performance, depen ...
- Jersey(1.19.1) - Life-cycle of Root Resource Classes
By default the life-cycle of root resource classes is per-request, namely that a new instance of a r ...
随机推荐
- Java 8 正式发布,新特性全搜罗
经过2年半的努力.屡次的延期和9个里程碑版本,甲骨文的Java开发团队终于发布了Java 8正式版本. Java 8版本最大的改进就是Lambda表达式,其目的是使Java更易于为多核处理器编写代码: ...
- php把时间格式化
如题,把如 2013-6-12 12:00 格式化为 2013-6--12 可以先将时间转换下,然后重新将时间格式化显示: echo date("Y-m-d", strtotime ...
- UI进阶 SQLite错误码
#define SQLITE_OK 0 /* 成功 | Successful result */ /* 错误码开始 */ #define SQLITE_ERROR 1 /* SQL错误 或 丢失数据库 ...
- Web.config之连接字介绍
一.连接字配置方式 web.config中有两种方式来配置连接字:<appsetting>中配置,<connectionStrings>中配置. 1.<appsettin ...
- Converting a .jks Key Store to a .pem Key Store
In order to convert a Java key store into a Privacy Enhanced Mail Certificate, you will need to use ...
- PostgreSQL中,database,schema,table之间关系
从逻辑上看,schema,table,都是位于database之下. 首先,在postgres数据库下建立表(相当于建立在public schema下): [pgsql@localhost bin]$ ...
- WIM更新命令(打补丁)
在D盘新建3个文件夹:win7(install.wim).updates(补丁).win7ultra 1.先打开ISO文件,然后加载映像到D:\win7ultra文件夹dism /mount-wim ...
- S5PV210开发系列四_uCGUI的移植
S5PV210开发系列四 uCGUI的移植 象棋小子 1048272975 GUI(图形用户界面)极大地方便了非专业用户的使用,用户无需记忆大量的命令,取而代之的是能够通过窗体.菜单 ...
- 为CentOS 加入�本地源
首先把光盘中的Packages文件夹复制到本地. [arm@Jarvis Packages]$ pwd /home/Packages 安装用于创建安装包依赖关系的软件createrepo. [arm@ ...
- MHA手动在线切换主 原创3(主不参与复制)
monitor 执行:slave2连接到slave1,server1 不做(主/从复制角色,停在那里) [root@monitor app1]# masterha_master_switch --co ...