Jersey 2.22.2 官方文档第7章学习笔记
2016-05-13 23:04
302 查看
官网文档地址:https://jersey.java.net/documentation/latest/representations.html#d0e6665
发出请求 http://localhost:9998/buildResp?content=1234 返回结果如下图所示。
![](http://img.blog.csdn.net/20160513224553227)
但上面的代码没有任何返回数据,返回数据可以通过ResponseBuilder的entity方法来添加。
![](http://img.blog.csdn.net/20160513224707696)
除了entity还可以设置其它的属性,如last modified等。
response来返回给客户端。
发出请求 http://localhost:9998/buildResp
![](http://img.blog.csdn.net/20160513224946353)
除了通过WebApplicationException映射异常,还可以通过实现 ExceptionMapper<E
extends Throwable>接口来映射异常。
![](http://img.blog.csdn.net/20160513225316964)
上面的代码带有@Provider注解。当系统抛出Exception异常,EntityNotFoundMapper的toResponse方法将被调用。Jersey支持异常映射器的扩展。扩展的异常映射器必须实现org.glassfish.jersey.spi.ExtendedExceptionMapper接口。这个接口继承与前面所提到的ExceptionMapper<T>接口,并提供了isMappable(Throwable)方法。该方法判断所提供的异常是否能被处理。当返回true时,表示能够处理,false表示不能处理。在运行时,当异常被抛出,该方法将被调用。因此创建ExtendedExceptionMapper子类,然后实现isMappable方法,有选择地对异常处理。
![](http://img.blog.csdn.net/20160513225611652)
修改isMappble方法后,异常被没有被映射处理。因此返回500,而不是返回定义的http响应码。
![](http://img.blog.csdn.net/20160513225840466)
因此当我们需要对某且异常映射处理进行拦截,并进行选择性处理时,实现ExtendedExceptionMapper接口即可。
第7章
7.2 创建Responses对象
有时,对于一个Request请求,需要返回额外的信息。这些信息能够通过使用Response对象与ResponseBuilder对象来创建并返回。例如,Restful新资源的创建是通过一个POST请求并返回201(状态码)以及响应头的形式来完成。其URI是新创建的资源。上述过程可以通过下面的代码来实现。@Path("buildResp") public class BuildingResponse { @GET @Consumes(MediaType.TEXT_PLAIN) public Response post(@QueryParam("content")String content){ URI createUri = URI.create("xxx.ll.com/ss"); return Response.created(createUri).build(); } }
发出请求 http://localhost:9998/buildResp?content=1234 返回结果如下图所示。
但上面的代码没有任何返回数据,返回数据可以通过ResponseBuilder的entity方法来添加。
@Path("buildResp") public class BuildingResponse { @GET @Consumes(MediaType.TEXT_PLAIN) public Response post(@QueryParam("content")String content){ URI createUri = URI.create("xxx.ll.com/ss"); return Response.created(createUri).entity(content).build(); } }
除了entity还可以设置其它的属性,如last modified等。
7.3 WebApplicationException 异常与异常转换为并通过Response返回
7.2节演示了如何返回Http response对象。当在try-catch中处理异常时,可以通过同样的方法返回HTTP 错误。为了与Java编程模型保持一致,JAX-RS允许建立Java异常与Http error response对象的映射关系。下面的代码演示了从resource方法中抛出CustomNotFoundException异常,并以Httpresponse来返回给客户端。
@Path("buildResp") public class BuildingResponse { @GET @Consumes(MediaType.TEXT_PLAIN) public Response post(@QueryParam("content") String content) { if (content.equals("1")) { throw new CustomNotFoundException("{msg:content null exception,code:100001}"); } URI createUri = URI.create("xxx.ll.com/ss"); return Response.created(createUri).entity(content).build(); } } public class CustomNotFoundException extends WebApplicationException { private static final long serialVersionUID = -5532594936351897109L; public CustomNotFoundException() { super(Response.status(404).build()); } public CustomNotFoundException(String message) { super(Response.status(404).entity(message).type(MediaType.APPLICATION_JSON).build()); } }
发出请求 http://localhost:9998/buildResp
除了通过WebApplicationException映射异常,还可以通过实现 ExceptionMapper<E
extends Throwable>接口来映射异常。
@Path("buildResp") public class BuildingResponse { @GET @Consumes(MediaType.TEXT_PLAIN) public Response post(@QueryParam("content") String content) { if (content.equals("1")) { throw new RuntimeException("{msg:content null exception,code:100001}"); } URI createUri = URI.create("xxx.ll.com/ss"); return Response.created(createUri).entity(content).build(); } } @Provider public class EntityNotFoundMapper implements ExceptionMapper<Exception>{ public Response toResponse(Exception exception) { // TODO Auto-generated method stub return Response.status(404).entity(exception.getMessage()).type("text/plain") .build(); } }
上面的代码带有@Provider注解。当系统抛出Exception异常,EntityNotFoundMapper的toResponse方法将被调用。Jersey支持异常映射器的扩展。扩展的异常映射器必须实现org.glassfish.jersey.spi.ExtendedExceptionMapper接口。这个接口继承与前面所提到的ExceptionMapper<T>接口,并提供了isMappable(Throwable)方法。该方法判断所提供的异常是否能被处理。当返回true时,表示能够处理,false表示不能处理。在运行时,当异常被抛出,该方法将被调用。因此创建ExtendedExceptionMapper子类,然后实现isMappable方法,有选择地对异常处理。
@Provider public class ExtendExceptionMapper implements ExtendedExceptionMapper<Exception>{ public Response toResponse(Exception exception) { return Response.status(404).entity("{status:failed,code:1002}").build(); } public boolean isMappable(Exception exception) { // TODO Auto-generated method stub if(exception instanceof NullPointerException){ return false; } return true; } } @Path("buildResp") public class BuildingResponse { @GET @Consumes(MediaType.TEXT_PLAIN) public Response post(@QueryParam("content") String content) { if (content.equals("1")) { throw new RuntimeException("{msg:content null exception,code:100001}"); } URI createUri = URI.create("xxx.ll.com/ss"); return Response.created(createUri).entity(content).build(); } }发起请求http://localhost:9998/buildResp?content=1
修改isMappble方法后,异常被没有被映射处理。因此返回500,而不是返回定义的http响应码。
public boolean isMappable(Exception
if(exception instanceof NullPointerException){ return true; } return false; }
因此当我们需要对某且异常映射处理进行拦截,并进行选择性处理时,实现ExtendedExceptionMapper接口即可。
相关文章推荐
- 团队冲刺第八、九、十天
- 如何正解理解和掌握大智慧之全局、局部变量详解(含数组,教课书上都没有的内容)
- Prime Palindromes
- 团队项目第九天
- hive基础操作
- [CareerCup] 18.13 Largest Rectangle of Letters
- 团队项目第八天
- 常见排序算法(一)
- 225. Implement Stack using Queues (用队列模拟栈)
- 一个新人理解正则表达式
- spring ioc原理(看完后大家可以自己写一个spring)
- Java静态方法,静态变量,初始化顺序 static
- 业务建模 之 闲话'闭包'与'原型继承'
- 团队项目第七天
- 最好用的技术是最基础的
- 团队项目第六天
- Servlet——ServletContext 对象
- 关于sql语句的limit与查询记录的下标
- 团队项目第五天
- 团队项目第四天