Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.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 jersey错误处理+;泛型响应对象_Java_Generics_Exception Handling_Jersey - Fatal编程技术网

Java jersey错误处理+;泛型响应对象

Java jersey错误处理+;泛型响应对象,java,generics,exception-handling,jersey,Java,Generics,Exception Handling,Jersey,我正在用泽西岛建造restapi。我的示例rest GET方法运行良好。 如果restapi方法my class(扩展了javax.ws.rs.ext.ExceptionMapper)中出现任何异常,请将其映射到RestapiResponse对象,并将其发送回客户端 @GET @Produces(MediaType.APPLICATION_JSON) @Path("/hello/{key}") public Orange sayHello(@PathParam("key") String key

我正在用泽西岛建造restapi。我的示例rest GET方法运行良好。 如果restapi方法my class(扩展了javax.ws.rs.ext.ExceptionMapper)中出现任何异常,请将其映射到RestapiResponse对象,并将其发送回客户端

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/hello/{key}")
public Orange sayHello(@PathParam("key") String key) {
   ...
}
我的RestapiResponse类需要如下内容:

class RestapiResponse {
    private int httpStatus;
    private int errorCode;
    private String message;
    private String link;
    private String description;
    private String errorStackTrace;
    private Class<T> response; <-- holds the response object of rest call

    // getters/setters are here

    // produces a new instance of class and set the response object 
    public static RestapiResponse getInstance(T response, Class<T> type) {
        this.response = response;
    }
}
class RestapiCaller {
    public static RestapiResponse get(String uri, Class<T> type) {
        // jersey rest client call
        Client client = ClientBuilder.newClient();
        ...
        Response response = builder.get();

        if (response.getStatus() == 200) {
            return RestapiResponse.getInstance(response.readEntity(type), type);
        else
            return response.readEntity(RestapiResponse.class);
    }
}

我想请求帮助以实现RestapiResponse.getInstance()和RestapiCaller.get()方法。

您需要帮助的具体部分是什么?你有什么特别的地方卡住了吗?你到底需要帮助哪一部分?你有什么特别的东西卡住了吗?
RestapiResponse r = RestapiCaller.get("host:port/.../api/...", Orange.class)
if (r.getHttpStatus() == 200) {
    Orange o = r.getResponse();
}

RestapiResponse r = RestapiCaller.get("host:port/.../api/...", Car.class)
if (r.getHttpStatus() == 200) {
    Car c = r.getResponse();
}