RequestContext
表示一个请求处理过程中的上下文信息,包含:HttpRequest
、HttpResponse
、Attributes
等
public interface RequestContext {
/**
* Obtains current {@code request}.
*
* @return request
*/
HttpRequest request();
/**
* Obtains current {@code response}.
*
* @return response
*/
HttpResponse response();
/**
* Obtains {@link Attributes} corresponding with current {@link RequestContext}.
*
* @return attributes
*/
Attributes attrs();
}
Tip
RequestContext
所属包名为io.esastack.restlight.core.context
HttpRequest
所属包名为io.esastack.restlight.core.core
HttpResponse
所属包名为io.esastack.restlight.core.core
Attributes
所属包名为esa.commons.collection
Mapping
表示一个请求匹配的条件, 用于确定一个请求是否能够路由到某个目标对对象。
eg.
curl -X GET http://localhost:8080/hello
Mapping.get("/hello");
curl -X POST http://localhost:8080/foo?a=1
Mapping.post("/foo")
.hasParam("a", "1");
curl -X GET -H "a:1" -H "Content-Type:application/json" -H "Accept:application/json" http://localhost:8080/foo?a=1
Mapping.get("/foo")
.hasParam("a", "1")
.noneParam("b", "1")
.hasHeader("a", "1")
.noneHeader("b", "1")
.consumes(MediaType.APPLICATION_JSON)
.produces(MediaType.APPLICATION_JSON);
Route
Route
中包含了一个Mapping
用于路由匹配, 一个请求都将期望路由到具体的一个Route
, 如果找不到任何一个Route
则响应一个404
, 同时一个Route
还负责请求本身的业务处理。
eg.
@Bean
public ManagementConfigure configure() {
Mapping mapping = Mapping.get("/foo");
Route route = Route.route(mapping)
.handle((ctx) -> {
ctx.response().entity("Hello Restlight!");
})
.onError(((ctx, error) -> {
// error occurred
}))
.onComplete(((ctx, t) -> {
// request completed
}));
}