简体   繁体   English

从jersey返回HTTP代码出错时的Chunked响应

[英]Return HTTP codes from jersey Chunked response when in error

I've got three services like those described with chunked response in the jersey documentation: https://jersey.java.net/documentation/latest/async.html 我有三个服务,比如在泽西文档中用chunked response描述的服务: https//jersey.java.net/documentation/latest/async.html

but I've got to add user access control and respond accordingly with 403 error codes but found no way on adding status to the response or build an error response like those in services returning entity body types. 但是我必须添加用户访问控制并相应地响应403错误代码,但是没有办法在响应中添加状态或者像返回实体主体类型的服务那样构建错误响应。

any ideas? 有任何想法吗?

You can return Response instead of ChunkedOutPut and then wrap ChunkedOutPut or error message inside your Response object as shown below. 您可以返回Response而不是ChunkedOutPut ,然后在您的Response对象中包装ChunkedOutPut或错误消息,如下所示。

@GET
public Response getChunkedResponse() throws IOException {
    // Check access here
    if (!authorized()) {
        Response.status(403).entity("No Access").build();
    } else {
        final ChunkedOutput<String> output = new ChunkedOutput<String>(String.class);

        new Thread() {
            public void run() {
                // Your operation which returns chunked output.
            }
        }.start();
        return Response.ok(output).build();
    }
}

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

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