SiteBricks :: CORE Framework in the Java Class Library Guide
Sitebricks is a Java library for building a web application.It provides easy -to -use API and annotations, allowing developers to quickly build a powerful web application.
In this article, we will introduce the core framework of Sitebricks and provide some Java code examples to help you better understand how to use this framework.
1. Introduce the sitebricks library
First, you need to add the sitebricks library to your Java project.It can be achieved by adding the following dependency items to the construction file of the project (such as Maven's Pom.xml or Gradle's built.gradle):
Maven:
<dependency>
<groupId>com.google.sitebricks</groupId>
<artifactId>sitebricks</artifactId>
<version>0.8.8</version>
</dependency>
Gradle:
groovy
implementation 'com.google.sitebricks:sitebricks:0.8.8'
2. Create a web application
Sitebricks provides an annotation called `@@Service` to mark the Java class as a web service.You can define methods for processing HTTP requests in this kind.
The following is a simple example of using Sitebricks:
import com.google.sitebricks.At;
import com.google.sitebricks.http.Get;
@At("/hello")
public class HelloWorldService {
@Get
public String sayHello() {
return "Hello, Sitebricks!";
}
}
In the above examples, the `HelloWorldService` class is marked as a web service and specifies the URL path through the@AT` annotation.The `@get` annotation was used on the` Sayhello` method, indicating that the method was used to handle the http get request.
3. Start the web server
To run the SiteBricks application, you need to start a embedded web server.Sitebricks can be used with Jetty or Servlet containers (such as Tomcat).
The following is an example code that uses Jetty to start the sitebricks application:
import com.google.sitebricks.SitebricksServletModule;
import com.google.sitebricks.SitebricksModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
public class Main {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
ServletContextHandler contextHandler = new ServletContextHandler();
contextHandler.setContextPath("/");
server.setHandler(contextHandler);
contextHandler.addServlet(MainServlet.class, "/*");
server.start();
server.join();
}
public static class MainServlet extends SitebricksServletModule {
@Override
protected void configureSitebricks() {
install(new SitebricksModule() {
@Override
protected void configureSitebricks() {
// Register your service class
scan(HelloWorldService.class.getPackage());
}
});
}
}
}
In the above code, we created a Jetty server and configured a `MainServlet` to handle all HTTP requests.In the `MainServlet`, we use Sitebricks'` sitebricksmodule` to configure the service -class scan.
4. Visit web applications
When your application runs on the web server, you can use your browser or command line tool to access it.In the above example, you can view the output of "Hello, Sitebricks!" By entering the `http:// localhost: 8080/Hello`.
Summarize
Sitebricks is a powerful and flexible Java library designed for building a web application.This article introduces the core framework of SiteBricks and provides a simple example to help you get started.I hope this article can help you better understand Sitebricks and use it in your project.