【问题标题】:What's Jersey's default exception handling? (if an ExceptionMapper is NOT provided)Jersey 的默认异常处理是什么? (如果没有提供 ExceptionMapper)
【发布时间】:2016-06-09 14:16:32
【问题描述】:

Jersey 的默认异常处理是什么(当未提供 ExceptionMapper 时)?

例子:

@GET
@Path("/rest")
public String rest() {
  throw new RuntimeException("Wonder what would happen...");
}

结果会是什么? HTTP 状态和内容会返回什么?

【问题讨论】:

  • 为什么要问什么时候可以尝试?
  • 这叫知识共享。问也很重要。让其他人有机会回答...

标签: java http jersey


【解决方案1】:

您的函数必须返回一个响应对象 (javax.ws.rs.core.Response)。

@GET
@Path("/rest")
public Response invokeSomething() {
   return Response.ok("Some string").build();
}

方法 ok 返回注解类中序列化对象的 202 响应。乙:

@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON})
public class ....

这个注解意味着我的服务使用一个 json 并且我的响应是一个 json。

然后,当您想要返回错误时,您必须返回代码 500。 在你的例子中:

    @GET
    @Path("/rest")
    public Response invokeSomething() {
       return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("exeption message").build();
    }

【讨论】:

    猜你喜欢
    • 2013-11-18
    • 1970-01-01
    • 2015-03-14
    • 2012-01-23
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    相关资源
    最近更新 更多