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. "