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 ...
随机推荐
- HDU 5832 A water problem (水题,大数)
题意:给定一个大数,问你取模73 和 137是不是都是0. 析:没什么可说的,先用char 存储下来,再一位一位的算就好了. 代码如下: #pragma comment(linker, "/ ...
- 【Java】多线程冲突解决——同步锁
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827547.html 解决并行冲突最有效的方法就是加同步锁,主要有以下几种方法: 1:动态方法 ...
- 对PostgreSQL中 pg_各表的RelationId的认识
读取普通的table或者系统表,都会调用heap_open函数: /* ---------------- * heap_open - open a heap relation by relation ...
- Cocos2d-x中由sprite来驱动Box2D的body运动(用来制作平台游戏中多变的机关)
好久都没写文章了,就来一篇吧.这种方法是在制作<胖鸟大冒险>时用到的.<胖鸟大冒险>中使用Box2D来进行物理模拟和碰撞检測,因此对每一个机关须要创建一个b2body.然后&l ...
- Codeforces gym 100685 C. Cinderella 水题
C. CinderellaTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/C ...
- webqq 获得好友列表hash算法 获得最新hash的方法
webqq获得好友列表的hash算法,大约每一个月中旬会变动一次.知道怎么获得他就能够了. js文件路径 http://web.qstatic.com/webqqpic/pubapps/0/50/eq ...
- [Javascript] Manipulate the DOM with the classList API
Learn how to add, remove and test for CSS classes using the classList API. It's more powerful than u ...
- [AngularJS] ui-router: Abstract States
ui-router has the powerful ability to define abstract states, or states that can't be navigated to, ...
- JVM自动内存管理学习笔记
对于使用 C.C++ 的程序员来说,在内存管理领域,他们既是拥有最高权力的皇帝又是从事最基础工作的劳动人民——拥有每一个对象的“所有权”,又担负着每一个对象生命开始到终结的维护责任.对于 Java 程 ...
- oc-10-函数与方法的区别
.函数和对象方法的区别 以-开头的方法就是对象方法(即必须实例化对象才能使用的方法) 如: -(void)Run; 区别: ()语法区别,并且对象方法都以-号开头,函数直接以返回值开头 ()对象方法的 ...