Blade - Java Web Framework -Guide

Welcome to the new generation MVC framework(Web)
The Blade is based on Java8 + Netty4 to create a lightweight, high-performance, simple, and elegant Web framework. Blade Framework is based on the Apache2 License.


Features

  • A new generation MVC framework that doesn't depend on other libraries
  • Get rid of SSH's bloated, modular design
  • The source is less than 500kb, learning it is also simple
  • RESTful-style routing design
  • Template engine support, view development more flexible
  • High performance, 100 concurrent qps 20w/s
  • Run the JAR package to open the web service
  • Streams-style API
  • CSRF and XSS defence
  • Basic Auth and Authorization
  • Supports plug-in extensions
  • Support web jars resources
  • Tasks based on cron expressions
  • Built-in a variety of commonly used middleware
  • Built-in JSON output
  • JDK8 +

Advantages

  • 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 a DSL grammar.
  • Easy deploy: supports maven package jar file running.

Getting Started

Create a basic Maven or Gradle project.

Maven
<dependency>
    <groupId>com.bladejava</groupId>
    <artifactId>blade-mvc</artifactId>
    <version>2.0.14.RELEASE</version>
</dependency>


Gradle

compile 'com.bladejava:blade-mvc:2.0.14.RELEASE'

Running a Blade Application

The blade is based upon Netty, an amazing asynchronous event-driven network application framework. Therefore, to run our Blade-based application we don't need any external Application Server or Servlet Container; the JRE will be enough

Write the main method and the Hello World:
public static void main(String[] args) {
    Blade.of().get("/", ctx -> ctx.text("Hello World")).start();
}
After that, the application will be accessible at the http://localhost:9000 URL.

Register Route By annotation

First of all, we need to create a Controller through the @Path annotation, which will be scanned by Blade during the startup.

We then need to use the route annotation related to the HTTP method we want to intercept:

@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();
    }
}

Get Form Parameters By annotation

@PostRoute("/save")
public void savePerson(@Param String username, @Param Integer age){
    System.out.println("username is:" + username + ", age is:" + age)
}


Get Path Parameters By annotation

@PostRoute("/save")
public void savePerson(@Param String username, @Param Integer age){
    System.out.println("username is:" + username + ", age is:" + age)
}


Get Header By Annotation

@GetRoute("header")
public void getHeader(@HeaderParam String Host){
    System.out.println("Host => " + Host);
}


Get Cookie By Annotation

@GetRoute("cookie")
public void getCookie(@CookieParam String UID){
    System.out.println("Cookie UID => " + UID);
}


Upload File By Annotation

@PostRoute("upload")
public void upload(@MultipartParam FileItem fileItem){
    // Save to new path
    fileItem.moveTo(new File(fileItem.getFileName()));
}


Render JSON By annotation

@GetRoute("users/json")
@JSON
public User printJSON(){
    return new User("biezhi", 18);
}


Custom Exception Handler

The blade has an exception handler already implemented by default; if you need to deal with custom exceptions, you can do it like follows.
@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);
        }
    }}


Change Server Port

There are three ways to modify the port: hard coding it, in a configuration file, and through a command-line parameter.
Hard Coding
Blade.of().listen(9001).start();

Configuration For application.properties
server.port=9001

Command Line
java -jar blade-app.jar --server.port=9001


For more details Visit Official Documentation

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete