Java Dropwizard应用程序中带有ChunkeOutput和JSON的Jersey

Java Dropwizard应用程序中带有ChunkeOutput和JSON的Jersey,java,json,jersey,dropwizard,chunked-encoding,Java,Json,Jersey,Dropwizard,Chunked Encoding,我在应用程序中使用Dropwizard 0.9.1,我有一个GET方法返回ChunkedOuput,如下所述。MediaType应该是APPLICATION_JSON,它可以工作,但结果不是有效的JSON 以下是Ressource的示例: @GET @Path("/chunktest") @Produces(MediaType.APPLICATION_JSON) public class AsyncResource { @GET public ChunkedOutput<M

我在应用程序中使用Dropwizard 0.9.1,我有一个GET方法返回ChunkedOuput,如下所述。MediaType应该是APPLICATION_JSON,它可以工作,但结果不是有效的JSON

以下是Ressource的示例:

@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
    }
}
我还尝试使用块分隔符,但是使用这个分隔符,我只能修复块JSON之间的“,”,但是我不知道如何插入开始/结束括号

{

}


有人知道如何解决这个问题吗?

关于
新的ChunkedOutput(MyCustomObject.class,“,”)

第二个参数的Javadoc:

@param chunkDelimiter custom chunk delimiter string. Must not be {code null}.
(对于泽西岛2号)

将其拼凑在一起:)

public类ChunkedOutput JSON扩展了ChunkedOutput{
私有布尔值isFirstChunk=true;
私人最终JsonSerializer JsonSerializer;
公共ChunkedOutputJson(JsonSerializer JsonSerializer){
super(String.class);
this.jsonSerializer=jsonSerializer;
}
public void writeChunk(T chunk)引发IOException{
if(isFirstChunk){
super.write(“[\n”);
isFirstChunk=false;
}否则{
super.write(“,\n”);
}
write(jsonSerializer.toJson(chunk));
}
@凌驾
public void close()引发IOException{
super.write(“\n]\n”);
super.close();
}
}

我在原始ChunkeOutput中不使用delimiter属性的原因是,它也会将它添加到最后一个元素之后,因此会破坏json格式(如果需要严格的话)。

您不能只执行
输出。将(“[”
写入
run()
方法的开头,并将
output.write(“]”
写入最后一个块吗?这与您上面提到的分隔符相结合,将把输出转换为JSON数组。将messageBodyWriter与上面提到的内容一起使用实际上是唯一的方法,我可以如何使其工作,但它仍然只是我的一种解决方法;-)@音乐政策怎么做?ChunkeOutput的类型不是String.class。。。
@param chunkDelimiter custom chunk delimiter string. Must not be {code null}.
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();
    }
}