简体   繁体   English

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

[英]What's Jersey's default exception handling? (if an ExceptionMapper is NOT provided)

What's Jersey's default exception handling (when ExceptionMapper is NOT provided)? Jersey的默认异常处理是什么(未提供ExceptionMapper时)?

Example: 例:

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

What would the result be? 结果将是什么? What would return in the HTTP status and content? HTTP状态和内容返回什么?

Your function have to return a Response object (javax.ws.rs.core.Response). 您的函数必须返回一个Response对象(javax.ws.rs.core.Response)。

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

The method ok return the 202 resoponse with Object serialised in annotation class. 该方法可以返回在注释类中序列化的Object的202响应。 Es: Es:

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

This annotation mean that my service consume a json and my response is a json. 此注释意味着我的服务使用json,而我的响应是json。

Then, when you want return an error you have to return a code 500. In your exemple: 然后,当您要返回错误时,必须返回代码500。在您的示例中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM