java - Dropwizard 应用程序中带有 ChunkedOutput 和 JSON 的 Jersey

标签 java json jersey dropwizard chunked-encoding

我在我的应用程序中使用 Dropwizard 0.9.1,并且我有一个返回 ChunkedOuput 的 GET 方法,如所述 here . 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"
}

我也尝试使用 block 定界符,但这样我只能修复 block 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 格式(如果你需要它严格的话)。

关于java - Dropwizard 应用程序中带有 ChunkedOutput 和 JSON 的 Jersey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33805258/

相关文章:

.net - 使用 JSON 从 AJAX 和 JQuery 调用简单的 Web 服务(.asmx 文件) - 解析错误

javascript - Node.js 和 Mongoose 返回 2D 查询

multithreading - 带有 Jersey 的异步 REST API

java - 带有 Tomcat 的简单 JAX-RS - 404 未找到(无 web.xml)

java - 检测 View 上的滑动手势和触摸事件

java - 这种情况下的最佳阵列

jquery - 如何使用 jQuery 或 JavaScript 在 JSON 中添加新对象?

java - JAX-RS:NoSuchMethodFound 异常 Jersey-media-moxy JSON

java - 使用 HttpURLConnection 时获取本地端口

java.rmi.MarshalException