complete
Allows for log and cleanup a request. It will be invoked after we send a response.
complete("*", "*", (req, rsp, cause) -> );
}
}
You are NOT allowed to modify the request and response objects. The
cause
is an
Optional with a
Throwable useful to identify problems.
The goal of the
complete
handler is to probably cleanup request object and log
responses.
A
complete
handler must to be registered before the actual handler you want to
intercept.
complete("*", "/path", (req, rsp, cause) -> );
get("/path", req ->
return "hello";
});
}
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline
is executed as long you don't generate a response.
example
Suppose you have a transactional resource, like a database connection. The next example shows
you how to implement a simple and effective transaction-per-request
pattern:
// start transaction);
// commit/rollback transaction
complete((req, rsp, cause) ->
// unbind connection from request
try(Connection connection = req.unset("connection"))
Transaction trx = connection.getTransaction();
if (cause.ifPresent())
trx.rollback();
} else
trx.commit();
}
}
});
// your transactional routes goes here
get("/my-trx-route", req ->
Connection connection = req.get("connection");
// work with connection
});
}
}