Jersey(1.19.1) - WebApplicationException and Mapping Exceptions to Responses
2016-04-16 22:20
381 查看
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
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:
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:
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
The following example shows the throwing of a
NotFoundExceptionfrom 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
toResponsemethod of the
EntityNotFoundMapperinstance will be invoked.
相关文章推荐
- AngularJS学习笔记(三) 单页面webApp和路由(ng-route)
- WebApp开发之--"rem"单位(转)
- WebApplicationInitializer (spring 3.x.x以上版本)
- 创建maevn项目的时候报错:maven-archetype-webapp:pom:RELEASE
- web中webAppRootKey作用
- spring设置webAppRootKey
- java web中如何获取spring容器中定义的bean----WebApplicationContext的使用
- 使用ionic开发webapp进行文件上传与下载
- webAPP前端必备知识
- WebApp中的页面生命周期及路由管理
- Eclipse+Maven创建webapp项目
- webAPP前端必备知识
- Spring3.1.1+Jersey2.2+ehcache实现WebApp与服务器接口交互获取令牌及校验过程
- 基于CSS3的WEBAPP横向滑动模式演化
- nodejs+ionic+cordova+intellijIdea搭建webApp环境
- (webapp)微信和safri 对于html5 部分功能不兼容,多选或单选下拉框去除边框无效果。
- java web error:Choose unique values for the 'webAppRootKey' context-param in your web.xml files
- 【F2E】移动端(webapp)开发性能优化方案
- SpringMVC中的DispatcherServlet (及WebApplicationContext部分)
- spring中的Log4jConfigListener作用和webapp.root的设置