Blade
:rocket: Lightning fast and elegant mvc framework for Java8
Install / Use
/learn @lets-blade/BladeREADME
What Is Blade?
Blade is a pursuit of simple, efficient Web framework, so that JavaWeb development becomes even more powerful, both in performance and flexibility.
If you like to try something interesting, I believe you will love it.
If you think it's good, you can support it with a star or by donating :blush:
Features
- [x] A new generation MVC framework that doesn't depend on other libraries
- [x] Get rid of SSH's bloated, modular design
- [x] Source is less than
500kb, learning it is also simple - [x] RESTful-style routing design
- [x] Template engine support, view development more flexible
- [x] High performance, 100 concurrent qps 20w/s
- [x] Run the
JARpackage to open the web service - [x] Streams-style API
- [x]
CSRFandXSSdefense - [x]
Basic AuthandAuthorization - [x] Supports plug-in extensions
- [x] Support webjars resources
- [x] Tasks based on
cronexpressions - [x] Built-in a variety of commonly used middleware
- [x] Built-in Response output
- [x] JDK8 +
Overview
» 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.<br/>
» Elegance: blade supports the RESTful style routing interface, has no invasive interceptors and provides the writing of a DSL grammar.<br/>
» Easy deploy: supports maven package jar file running.<br/>
Quick Start
Create a basic Maven or Gradle project.
Do not create a
webappproject, Blade does not require much trouble.
Run with Maven:
<dependency>
<groupId>com.hellokaton</groupId>
<artifactId>blade-core</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
or Gradle:
compile 'com.hellokaton:blade-core:2.1.2.RELEASE'
Write the main method and the Hello World:
public static void main(String[] args) {
Blade.create().get("/", ctx -> ctx.text("Hello Blade")).start();
}
Open http://localhost:9000 in your browser to see your first Blade application!
Contents
Register RouteRequest ParameterGet EnvironmentGet HeaderGet CookieStatic ResourceUpload FileDownload FileSet SessionRender To BrowserRender TemplateRedirectsWrite CookieWeb HookLoggingBasic AuthChange Server PortConfiguration SSLCustom Exception Handler
Register Route
HardCode
public static void main(String[] args) {
// Create multiple routes GET, POST, PUT, DELETE using Blade instance
Blade.create()
.get("/user/21", getting)
.post("/save", posting)
.delete("/remove", deleting)
.put("/putValue", putting)
.start();
}
Controller
@Path
public class IndexController {
@GET("/login")
public String login(){
return "login.html";
}
@POST(value = "/login", responseType = ResponseType.JSON)
public RestResponse doLogin(RouteContext ctx){
// do something
return RestResponse.ok();
}
}
Request Parameter
URL Parameter
Using RouteContext
public static void main(String[] args) {
Blade.create().get("/user", ctx -> {
Integer age = ctx.queryInt("age");
System.out.println("age is:" + age);
}).start();
}
Using @Query annotation
@GET("/user")
public void savePerson(@Query Integer age){
System.out.println("age is:" + age);
}
Test it with sample data from the terminal
curl -X GET http://127.0.0.1:9000/user?age=25
Form Parameter
Here is an example:
Using RouteContext
public static void main(String[] args) {
Blade.create().get("/user", ctx -> {
Integer age = ctx.fromInt("age");
System.out.println("age is:" + age);
}).start();
}
Using @Form Annotation
@POST("/save")
public void savePerson(@Form String username, @Form Integer age){
System.out.println("username is:" + username + ", age is:" + age);
}
Test it with sample data from the terminal
curl -X POST http://127.0.0.1:9000/save -F username=jack -F age=16
Path Parameter
Using RouteContext
public static void main(String[] args) {
Blade blade = Blade.create();
// 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();
}
Using @PathParam Annotation
@GET("/users/:username/:page")
public void userTopics(@PathParam String username, @PathParam Integer page){
System.out.println("username is:" + usernam + ", page is:" + page);
}
Test it with sample data from the terminal
curl -X GET http://127.0.0.1:9000/users/hellokaton/2
Body Parameter
public static void main(String[] args) {
Blade.create().post("/body", ctx -> {
System.out.println("body string is:" + ctx.bodyToString());
}).start();
}
Using @Body Annotation
@POST("/body")
public void readBody(@Body String data){
System.out.println("data is:" + data);
}
Test it with sample data from the terminal
curl -X POST http://127.0.0.1:9000/body -d '{"username":"hellokaton","age":22}'
Parse To Model
This is the User model.
public class User {
private String username;
private Integer age;
// getter and setter
}
By Annotation
@POST("/users")
public void saveUser(@Form User user) {
System.out.println("user => " + user);
}
Test it with sample data from the terminal
curl -X POST http://127.0.0.1:9000/users -F username=jack -F age=16
Custom model identification
@POST("/users")
public void saveUser(@Form(name="u") User user) {
System.out.println("user => " + user);
}
Test it with sample data from the terminal
curl -X POST http://127.0.0.1:9000/users -F u[username]=jack -F u[age]=16
Body Parameter To Model
@POST("/body")
public void body(@Body User user) {
System.out.println("user => " + user);
}
Test it with sample data from the terminal
curl -X POST http://127.0.0.1:9000/body -d '{"username":"hellokaton","age":22}'
Get Environment
Environment environment = WebContext.blade().environment();
String version = environment.get("app.version", "0.0.1");
Get Header
By Context
@GET("header")
public void readHeader(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
@GET("header")
public void readHeader(@Header String host){
System.out.println("Host => " + host);
}
Get Coo
Related Skills
node-connect
334.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
82.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
334.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
82.1kCommit, push, and open a PR
