Based on Java8
+ Netty4
to create lightweight, high-performance, simple and elegant Web framework 😋
Spend 1 hour to learn it to do something interesting, a Spring in addition to the framework of the best choice.
🐾 Quick Start | 📘 Blade In Action | 🎬 Video Tutorial | 🌚 Contribution | 💰 Donate | 🇨🇳 简体中文
Blade
is a pursuit of simple, efficient Web framework, so that JavaWeb
development even more powerful, both in performance and flexibility.
If you like to try something interesting, I believe you will love it.
If you think this item is good can star support or donate it 😊
- A new generation of MVC frameworks that do not depend on more libraries
- Get rid of SSH's bloated, modular design
- Source less than
500kb
, learning is also simple - Restful style routing design
- Template engine support, view development more flexible
- High performance, 100 concurrent qps 14w/s
- Run the
JAR
package to open the web service - Streaming API style
-
CSRF
andXSS
defense -
Basic Auth
andAuthorization
- Supports plug-in extensions
- Support webjars resources
- Based on
cron
expression of tasks - Built-in a variety of commonly used middleware
- Built-in JSON output
- JDK8 +
» Simplicity: The design is simple, easy to understand and doesn't introduce many layers between you and the standard library. The goal of this project is that the users should be able to understand the whole framework in a single day.
» Elegance: blade
supports the RESTful style routing interface, has no invasive interceptors and provides the writing of DSL grammar.
» Easy deploy: support maven
package jar
file running.
Run with Maven
:
Create a basic Maven
project
<dependency>
<groupId>com.bladejava</groupId>
<artifactId>blade-mvc</artifactId>
<version>2.0.10.RELEASE</version>
</dependency>
Do not create a
webapp
project, blade is not so much trouble.
or Gradle
:
compile 'com.bladejava:blade-mvc:2.0.10.RELEASE'
Write main
method, try Hello World
:
public static void main(String[] args) {
Blade.of().get("/", ctx -> ctx.text("Hello Blade")).start();
}
Using browser open http://localhost:9000 so you can see the first Blade
application!
Register Route
Get Request Parameters
Get Environment
Get Header
Get Cookie
Static Resource
Upload File
Set Session
Render To Browser
Render Template
Redirects
Write Cookie
Web Hook
Logging
Basic Auth
Change Server Port
Configuration SSL
Custom Exception Handler
public static void main(String[] args) {
// Create Blade,using GET、POST、PUT、DELETE
Blade.of()
.get("/user/21", getting)
.post("/save", posting)
.delete("/remove", deleting)
.put("/putValue", putting)
.start();
}
@Path
public class IndexController {
@GetRoute("signin")
public String signin(){
return "signin.html";
}
@PostRoute("signin")
@JSON
public RestResponse doSignin(RouteContext ctx){
// do something
return RestResponse.ok();
}
}
Here is an example:
By Context
public static void main(String[] args) {
Blade.of().get("/user", ctx -> {
Integer age = ctx.queryInt("age");
System.out.println("age is:" + age);
}).start();
}
By Annotation
@PostRoute("/save")
public void savePerson(@Param String username, @Param Integer age){
System.out.println("username is:" + username + ", age is:" + age)
}
The terminal sends a data test
curl -X GET http://127.0.0.1:9000/user?age=25
curl -X POST http://127.0.0.1:9000/save -F username=jack -F age=16
By RouteContext
public static void main(String[] args) {
Blade blade = Blade.of();
// Create a route: /user/:uid
blade.get("/user/:uid", ctx -> {
Integer uid = ctx.pathInt("uid");
ctx.text("uid : " + uid);
});
// Create two parameters route
blade.get("/users/:uid/post/:pid", ctx -> {
Integer uid = ctx.pathInt("uid");
Integer pid = ctx.pathInt("pid");
String msg = "uid = " + uid + ", pid = " + pid;
ctx.text(msg);
});
// Start blade
blade.start();
}
By Annotation
@GetRoute("/users/:username/:page")
public void userTopics(@PathParam String username, @PathParam Integer page){
System.out.println("username is:" + usernam + ", page is:" + page)
}
The terminal sends a data test
curl -X GET http://127.0.0.1:9000/users/biezhi/2
public static void main(String[] args) {
Blade.of().post("/body", ctx -> {
System.out.println("body string is:" + ctx.bodyToString())
}).start();
}
The terminal sends a data test
curl -X POST http://127.0.0.1:9000/body -d '{"username":"biezhi","age":22}'
This is User
model.
public class User {
private String username;
private Integer age;
// getter and setter
}
By Annotation
@PostRoute("/users")
public void saveUser(@Param User user){
System.out.println("user => " + user);
}
The terminal sends a data test
curl -X POST http://127.0.0.1:9000/users -F username=jack -F age=16
Custom model identification
@PostRoute("/users")
public void saveUser(@Param(name="u") User user){
System.out.println("user => " + user);
}
The terminal sends a data test
curl -X POST http://127.0.0.1:9000/users -F u[username]=jack -F u[age]=16
Body Parameter To Model
public void getUser(@BodyParam User user){
System.out.println("user => " + user);
}
The terminal sends a data test
curl -X POST http://127.0.0.1:9000/body -d '{"username":"biezhi","age":22}'
Environment environment = WebContext.blade().environment();
String version = environment.get("app.version", "0.0.1");;
By Context
@GetRoute("header")
public void getHeader(RouteContext ctx){
System.out.println("Host => " + ctx.header("Host"));
// get useragent
System.out.println("UserAgent => " + ctx.userAgent());
// get client ip
System.out.println("Client Address => " + ctx.address());
}
By Annotation
@GetRoute("header")
public void getHeader(@HeaderParam String Host){
System.out.println("Host => " + Host);
}
By Context
@GetRoute("cookie")
public void getCookie(RouteContext ctx){
System.out.println("UID => " + ctx.cookie("UID"));
}
By Annotation
@GetRoute("cookie")
public void getCookie(@CookieParam String UID){
System.out.println("Cookie UID => " + UID);
}
Blade built a few static resource catalog, as long as you will save the resource file in the static directory under the classpath, and then browse http://127.0.0.1:9000/static/style.css
If you want to customize the static resource URL.
Blade.of().addStatics("/mydir");
Of course you can also specify in the configuration file. application.properties
(location in classpath)
mvc.statics=/mydir
By Request
@PostRoute("upload")
public void upload(Request request){
request.fileItem("img").ifPresent(fileItem -> {
byte[] data = fileItem.getData();
// Save the temporary file to the specified path
Files.write(Paths.get(filePath), data);
});
}
By Annotation
@PostRoute("upload")
public void upload(@MultipartParam FileItem fileItem){
byte[] data = fileItem.getData();
// Save the temporary file to the specified path
Files.write(Paths.get(filePath), data);
}
public void login(Session session){
// if login success
session.attribute("login_key", SOME_MODEL);
}
By Context
@GetRoute("users/json")
public void printJSON(RouteContext ctx){
User user = new User("biezhi", 18);
ctx.json(user);
}
By Annotation
This form looks more concise 😶
@GetRoute("users/json")
@JSON
public User printJSON(){
return new User("biezhi", 18);
}
@GetRoute("text")
public void printText(RouteContext ctx){
ctx.text("I Love Blade!");
}
@GetRoute("html")
public void printHtml(RouteContext ctx){
ctx.html("<center><h1>I Love Blade!</h1></center>");
}
By default all template files are in the templates directory, most of the cases you do not need to change it.
By default, the Blade uses the built-in template engine, which is very simple if you really do a web project can try several other extensions.
public static void main(String[] args) {
Blade.of().get("/hello", ctx -> {
ctx.attribute("name", "biezhi");
ctx.render("hello.html");
}).start(Hello.class, args);
}
The hello.html
template
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello Page</title>
</head>
<body>
<h1>Hello, ${name}</h1>
</body>
</html>
Config Jetbrick Template
Create a BladeLoader
class load some config
@Bean
public class TemplateConfig implements BladeLoader {
@Override
public void load(Blade blade) {
blade.templateEngine(new JetbrickTemplateEngine());
}
}
Write some data for the template engine to render
public static void main(String[] args) {
Blade.of().get("/hello", ctx -> {
User user = new User("biezhi", 50);
ctx.attribute("user", user);
ctx.render("hello.html");
}).start(Hello.class, args);
}
The hello.html
template
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello Page</title>
</head>
<body>
<h1>Hello, ${user.username}</h1>
#if(user.age > 18)
<p>Good Boy!</p>
#else
<p>Gooood Baby!</p>
#end
</body>
</html>
@GetRoute("redirect")
public void redirectToGithub(RouteContext ctx){
ctx.redirect("https://github.com/biezhi");
}
@GetRoute("write-cookie")
public void writeCookie(RouteContext ctx){
ctx.cookie("hello", "world");
ctx.cookie("UID", "22", 3600);
}
WebHook
is the interface in the Blade framework that can be intercepted before and after the execution of the route.
public static void main(String[] args) {
// All requests are exported before execution before
Blade.of().before("/*", ctx -> {
System.out.println("before...");
}).start();
}
Blade using slf4-api as a log interface, the default implementation of a simple log package (modified from simple-logger), if you need complex logging you can also use custom, you only need to exclude the blade-log
in dependencies.
private static final Logger log = LoggerFactory.getLogger(Hello.class);
public static void main(String[] args) {
log.info("Hello Info, {}", "2017");
log.warn("Hello Warn");
log.debug("Hello Debug");
log.error("Hello Error");
}
Blade built a few middleware, when you need Basic certification can be used, of course, can also be customized to achieve.
public static void main(String[] args) {
Blade.of().use(new BasicAuthMiddleware()).start();
}
Specify the user name and password in the application.properties
configuration file.
http.auth.username=admin
http.auth.password=123456
There are three ways to modify the port, hard coding, configuration files, start the command line parameters.
Hard Coding
Blade.of().listen(9001).start();
Configuration For application.properties
server.port=9001
Command Line
java -jar blade-app.jar --server.port=9001
Configuration For application.properties
server.ssl.enable=true
server.ssl.cert-path=cert.pem
server.ssl.private-key-path=private_key.pem
server.ssl.private-key-pass=123456
Blade has already implemented an exception handler by default, and sometimes you need to deal with custom exceptions, so you can do it.
@Bean
public class GlobalExceptionHandler extends DefaultExceptionHandler {
@Override
public void handle(Exception e) {
if (e instanceof CustomException) {
CustomException customException = (CustomException) e;
String code = customException.getCode();
// do something
} else {
super.handle(e);
}
}
}
How easy it all looks, but the features above are the tip of the iceberg, and there are more surprises to see in the documentation and sample projects:
- Twitter: biezhi
- Mail: biezhi.me@gmail.com
Thanks goes to these wonderful people
王爵nice |
ccqy66 |
王晓辉(Eddie) |
代码家 |
David Dong |
José Vieira Neto |
Schneeman |
---|---|---|---|---|---|---|
Mohd Farid |
sumory |
Uday K |
Antony Kwok |
Contributions of any kind are welcome!
Please see Apache License