Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jax-RS:从REST调用获取错误消息?_Java_Rest_Error Handling_Jax Rs_Postman - Fatal编程技术网

Java Jax-RS:从REST调用获取错误消息?

Java Jax-RS:从REST调用获取错误消息?,java,rest,error-handling,jax-rs,postman,Java,Rest,Error Handling,Jax Rs,Postman,我正在使用Jax-RS作为我的REST框架。如果rest调用失败(即出现500个错误),我希望能够从rest调用中提取错误消息。最好的方法是什么 例如,当我在邮递员中打电话时,发现一个错误,我用500得到以下信息: { "reason": "No data retrieved for GET call", "response": "failure" } 在调用失败的情况下,如何在代码中检索此信息?如果您要查找的是解析该信息: 该响应是一种称为JSON(JavaScript对象表

我正在使用
Jax-RS
作为我的
REST
框架。如果rest调用失败(即出现500个错误),我希望能够从rest调用中提取错误消息。最好的方法是什么

例如,当我在
邮递员
中打电话时,发现一个错误,我用500得到以下信息:

{
    "reason": "No data retrieved for GET call",
    "response": "failure"
}

在调用失败的情况下,如何在代码中检索此信息?

如果您要查找的是解析该信息:

该响应是一种称为
JSON
(JavaScript对象表示法)的文本格式

使用
JSON
解析库,例如

使用
ApiResponse
看起来像:

public class APIResponse {

    public String reason;
    public String response;

    public void setReason(String reason) {
        this.reason = reason;
    }

    public String getReason() {
        return this.reason;
    }

    public void setReason(String reason) {
        this.response = response;
    }

    public String getResponse() {
        return this.response;
    }
}

另一方面,如果这是来自API的响应,并且您希望处理它:请查看

您可以实现
ExceptionMapper
界面:

public interface ExceptionMapper<E extends Throwable> {
   Response toResponse(E exception);
}

你是什么意思?你如何检索代码?解析json并读取原因?在此之前,我收到一个500错误,我现在将编辑我的问题,请编辑并发布示例。
public interface ExceptionMapper<E extends Throwable> {
   Response toResponse(E exception);
}
@Provider
public class EntityNotFoundMapper implements ExceptionMapper<Exception> {

   public Response toResponse(Exception e) {
      // Do some logic like log an error
      return Response.status(Response.Status.NOT_FOUND).build();
   }
}