Why am i getting "java.lang.IllegalStateException: Response has already been written"

1,554 views
Skip to first unread message

Delafosse Paul

unread,
Aug 12, 2018, 3:18:45 PM8/12/18
to vert.x
Hello everyone, 

I am trying to read from the following file tree and append the metadata.json files to the HTTP response:

|-- presentaitons 
|   |-- presentation1
        |-- metadata.json
|   |-- presentation2
        |-- metadata.json

I have a handler raising the following error : "java.lang.IllegalStateException: Response has already been written" 



private void getMetadataFromFileList(RoutingContext context) {
    this.fileSystem.readDir(presentationPath, listAsyncResult -> {
if (listAsyncResult.succeeded()) {
listAsyncResult.result().forEach(file -> {
String[] split = file.split("/");
String presentationName = split[split.length - 1];

this.fileSystem.readFile(this.presentationPath+"/" + presentationName + "/metadata.json", res -> {
if (res.succeeded()) {
context.response().write(res.result());
} else {
logger.error("Could not read " + presentationName + " metadata.");
}
});
});
} else {
logger.error(listAsyncResult.cause().getMessage());
context.response().setStatusCode(500);
context.response().end();
}
context.response().putHeader("content-type", "aplication/json");
context.response().setStatusCode(200);
context.response().end();
});
}


I am fairly new to vertx and i don't understand what's happening. According to the documentation write() "
can be invoked multiple times before the response is ended. "

Prajeesh K P

unread,
Aug 13, 2018, 12:03:59 AM8/13/18
to ve...@googlegroups.com
Hi Paul,

From the above code the one scenario of this happening is that the code flow is entering the else block where the response is ended and when you try to end it after if else block there this error is occuring.

This might be one of the scenario. For fixing it you can try to end the success case inside the if block itself.

Regards

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/65945d2d-9d87-4cc1-a36e-2b862f789625%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Delafosse Paul

unread,
Aug 13, 2018, 1:44:50 AM8/13/18
to vert.x
Actually i am the getting the error without the else block. 

Grey Seal

unread,
Aug 13, 2018, 6:44:01 AM8/13/18
to vert.x
Try moving the last 3 lines inside the handler. 

private void 
    this.fileSystem.readDir(presentationPath, listAsyncResult -> {
if (listAsyncResult.succeeded()) {
listAsyncResult.result().forEach(file -> {
String[] split = file.split("/");
String presentationName = split[split.length - 1];

this.fileSystem.readFile(this.presentationPath+"/" + presentationName + "/metadata.json", res -> {
if (res.succeeded()) {
context.response().write(res.result());
//moved the below 3 lines from down
                       context.response().putHeader("content-type", "aplication/json");
context.response().setStatusCode(200);
context.response().end();
} else {
logger.error("Could not read " + presentationName + " metadata.");
}
});
});
} else {
logger.error(listAsyncResult.cause().getMessage());
context.response().setStatusCode(500);
context.response().end();
}
    });
}

zaman.i...@gmail.com

unread,
Aug 13, 2018, 6:50:53 AM8/13/18
to vert.x
Hi Paul,

So you have to understand that you are writing a response when the handler for fileHandler.open() succeeds, the handler succeeds asynchronously anytime is the future. Since vertx is non-blocking it executes the last three lines of the code and you write a response there. Now after a certain time period, the handler succeeds and it tries to write the response again, that is why the error.

Delafosse Paul

unread,
Aug 14, 2018, 4:22:58 AM8/14/18
to vert.x
I think i got it, thank you! I managed to get it work using blocking functions. I just wonder if there is a way to accomplish it asynchronously?
Reply all
Reply to author
Forward
0 new messages