简体   繁体   中英

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

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.

any ideas?

You can return Response instead of ChunkedOutPut and then wrap ChunkedOutPut or error message inside your Response object as shown below.

@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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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