Frequently Asked Questions of SiteBricks :: CORE framework
Sitebricks is an open source Java Web framework, which provides simple and powerful tools and functions to build a modern web application.This article will answer some common questions around Sitebricks, and comes with Java code examples.
Question 1: What is the core framework of Sitebricks?
Answer: The core framework of Sitebricks is an annotated web framework. It uses Google Guice to depend on injection container and Jetty server.It provides simple API and functions that help developers to quickly build a RESTFUL -style web application.
Question 2: How to define a route in sitebricks?
Answer: In SiteBricks, you can use @AT annotation to define a route.The following is an example:
import com.google.sitebricks.At;
import com.google.sitebricks.http.Get;
@At("/hello")
public class HelloResource {
@Get
public String hello() {
return "Hello, Sitebricks!";
}
}
In the above example, we define a class called HelloResource and use @AT annotation to map it to the path "/Hello".On the hello () method of this class, we use @Get annotations to specify this method to process the HTTP GET request and return a simple string.
Question 3: How to deal with URL path parameters?
Answer: In SiteBricks, you can capture the URL path parameter by using a placeholder in the routing definition.The following is an example:
@At("/hello/{name}")
public class HelloResource {
@Get
public String hello(@Param("name") String name) {
return "Hello, " + name + "!";
}
}
In the above example, we use the place occupying "{name}" in the routing definition, and use @Param annotations in the HELLO () method to specify the name of the path parameter to be captured.Inside the method, we can access the actual value in the URL through this parameter.
Question 4: How to process the POST request and receive data in the request body?
Answer: In Sitebricks, you can use @Post and @Accept labels to process the post request, and use @wrapwith annotations to specify the data in the request body.The following is an example:
@At("/user")
public class UserResource {
@Post
public void createUser(@WrapWith(User.class) User user) {
// Treatment of user creation logic
}
}
public class User {
private String name;
private int age;
// omit the getter and setter method
}
In the above example, we define a class called UserResource, using @AT annotation to map it to the path "/user".On the CreateUser () method, we use @post annotations to specify this method to handle the http post request.In terms of method parameters, we use the @Wrapwith annotation to bind the data in the request to a class called User.
Question 5: How to extract the query parameters from the request?
Answer: In SiteBricks, you can use @Param annotations to extract query parameters.The following is an example:
@At("/search")
public class SearchResource {
@Get
public void search(@Param("query") String query) {
// Process query logic
}
}
In the above example, we define a class called SearchResource, using @AT Annotation to map it to the path "/search".On the search () method parameters, we use @Param annotations and specify the name of the query parameter.Inside the method, we can access the actual value of the query parameter through this parameter.
It is hoped that this article will help understand and use the core framework of Sitebricks.Through the above questions and code examples, you can better start building a modern web application in Sitebricks.