繁体   English   中英

在 Dropwizard 应用程序中使用 ChunkedOutput 和 JSON 的 Jersey

[英]Jersey with ChunkedOutput and JSON in Dropwizard Application

我在我的应用程序中使用 Dropwizard 0.9.1 并且我有一个 GET-Method 返回一个像这里描述的 ChunkedOuput 。 MediaType 应该是 APPLICATION_JSON 并且它可以工作,但结果不是有效的 JSON。

这是示例资源:

@GET
@Path("/chunktest")
@Produces(MediaType.APPLICATION_JSON)
public class AsyncResource {
    @GET
    public ChunkedOutput<MyCustomObject> getChunkedResponse() {
        final ChunkedOutput<MyCustomObject> output = new ChunkedOutput<MyCustomObject>(MyCustomObject.class);

        new Thread() {
            public void run() {
                try {
                    MyCustomObject chunk;

                    while ((chunk = getNextCustomObject()) != null) {
                        output.write(chunk);
                    }
                } catch (IOException e) {
                    // IOException thrown when writing the
                    // chunks of response: should be handled
                } finally {
                    output.close();
                        // simplified: IOException thrown from
                        // this close() should be handled here...
                }
            }
        }.start();

        // the output will be probably returned even before
        // a first chunk is written by the new thread
        return output;
    }

    private MyCustomObjectgetNextCustomObject() {
        // ... long running operation that returns
        //     next object or null
    }
}

现在,如果我尝试 curl 会返回这个无效的 JSON:

HTTP/1.1 200 OK
Date: Thu, 19 Nov 2015 13:08:28 GMT
Content-Type: application/json
Vary: Accept-Encoding
Transfer-Encoding: chunked

{
   "key1" : "value1a",
   "key2" : "value2a"
}{
   "key1" : "value1b",
   "key2" : "value2b"
}{
   "key1" : "value1c",
   "key2" : "value2c"
}{
   "key1" : "value1d",
   "key2" : "value2d"
}

我也尝试使用块分隔符,但这样我只能修复块 JSON 之间的“,”,但我不知道如何插入开始/结束括号

{

}

有谁知道如何解决这一问题?

一起破解这个:)

public class ChunkedOutputJson<T> extends ChunkedOutput<String> {
    private boolean isFirstChunk = true;
    private final JsonSerializer jsonSerializer;
    public ChunkedOutputJson(JsonSerializer jsonSerializer) {
        super(String.class);
        this.jsonSerializer = jsonSerializer;
    }
    public void writeChunk(T chunk) throws IOException {
        if (isFirstChunk) {
            super.write("[\n");
            isFirstChunk = false;
        } else {
            super.write(",\n");
        }
        super.write(jsonSerializer.toJson(chunk));
    }
    @Override
    public void close() throws IOException {
        super.write("\n]\n");
        super.close();
    }
}

我没有在原始 ChunkedOutput 中使用 delimiter 属性的原因是它也会在最后一个元素之后添加它,因此搞砸了 json 格式(如果你需要严格的话)。

new ChunkedOutput<MyCustomObject>(MyCustomObject.class, ",")怎么样?

第二个参数的 Javadoc:

@param chunkDelimiter custom chunk delimiter string. Must not be {code null}.

(对于球衣 2)

暂无
暂无

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

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