Summer is a simple mvc lib base on Netty4.x for study!!!
- quick start
public class Application {
public static void main(String[] args) {
Summer.me()
.before("/example/*", (request, response) -> {
log.debug("path: {}", request.path());
// pass
return true;
})
.get("/example", (request, response) -> response.json(Result.of("summer *_*!!!")))
.get("/example/:id", (request, response) -> response.text(request.paths().get("id")))
.post("/example/:id", (request, response) -> response.text(request.paths().get("id")))
.put("/example/:id", (request, response) -> response.text(request.paths().get("id")))
.delete("/example/:id", (request, response) -> response.text(request.paths().get("id")))
.listen(9000)
.serve();
}
}
- custom
public class Application {
public static void main(String[] args) {
ExampleController controller = new ExampleController();
// summer instance
Summer summer = Summer.me();
Router router = summer.router();
// custom notFound handler
router.notFound((request, response) -> response.text("404"));
// custom failure handler
router.failureHandler((request, response, t) -> response.text("500"));
// register routes
router.get("/example/text", controller::text);
router.get("/example/json", controller::json);
// http server listen on 9000 and serve
summer.listen(9000).serve();
}
}
As you can see, :name
is a named parameter. You can get the value of a parameter by request.paths().get("name")
method.
Pattern: /user/:name
/user/zhangsan match
/user/lisi match
/user/wangwu/zhaoliu no match
/user/ no match
Note: :name
name must be [a-zA-Z], otherwise will be matched exactly!
Ant pattern:
?
match a character*
match one or more characters**
match one or more directories
method request.body()
support Content-Type
:
multipart/form-data
application/x-www-form-urlencoded
As for application/json
, use method request.json()
instead of.
request.files()
request.file(name)
Then you can use FileUpload
for all operations.
response.text(text)
response.json(json)
Content-Type
(text/plain
and application/json
) will be added.
Now, easy to use:
response.sendFile(file)
response.redirect(targetUrl)
will set http status 302
, and add Location
on http response header.
Summers.summer()
// static resource
.staticFile("/static", "/developer/Code/summer")
.listen(9000)
.serve();
For example, request http://ip:9000/static/some.txt
will be mapped to the local file path: /developer/Code/summer/some.txt