Explore the complete Java documentation for the Ra framework here.
To get started, add the following dependency to your pom.xml
:
<dependency>
<groupId>org.racore</groupId>
<artifactId>ra-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
This guide demonstrates how to use the Ra framework for building REST APIs, including handling HTTP methods, file uploads, and serving static files. Let's dive in with examples based on a Person object.
Create an endpoint effortlessly with the following syntax:
verb("/endpoint", request -> {
// Process the request (parse the request body into an object, access query parameters, path variables, files, or form data, etc.)
// Return an object or a file path to be resolved
return new MyObject();
});
Start up a web server, register an endpoint, and return a JSON response in one line:
get("/getPerson/{id}", _ -> new Person("Alice", 30));
- Purpose: Return a JSON response for a
Person
object. - Output:
{ "name": "Alice", "age": 30 }
get("/getPathVariables/{id}/{name}", Request::getPathVariables);
- Purpose: Extract path variables from the URL.
- Example:
- Request:
/getPathVariables/12/Jay
- Response:
{ "param0": "12", "param1": "Jay" }
- Request:
get("/getQueryParameters", Request::getQueryParams);
- Purpose: Extract query parameters from the URL.
- Example:
- Request:
/getQueryParameters?name=John&age=25
- Response:
{ "name": "John", "age": "25" }
- Request:
post("/addPerson", request -> {
Person person = request.getBodyAs(Person.class);
return "Received Person: " + person;
});
- Purpose: Accept JSON payloads to create a new person.
- Example:
- Request Body:
{ "name": "Alice", "age": 30 }
- Response:
Received Person: Person{name='Alice', age=30}
- Request Body:
put("/updatePerson", request -> {
Person person = request.getBodyAs(Person.class);
return "Updated Person: " + person;
});
- Purpose: Update an entire
Person
object. - Example:
- Request Body:
{ "name": "Alice", "age": 35 }
- Response:
Updated Person: Person{name='Alice', age=35}
- Request Body:
delete("/deletePerson", _ -> "Person deleted");
- Purpose: Delete a person.
- Output:
Person deleted
post("/uploadFile", request -> "Files Received: " + request.getUploadedFiles().size());
- Purpose: Handle file uploads.
- Example:
- Request: Upload multiple files.
- Response:
Files Received: 3
post("/sendForm", request -> "Form data received: " + request.getFormFields());
- Purpose: Process form data submitted via
POST
. - Example:
- Request Body: Form data with fields
name=John
andage=25
. - Response:
Form data received: {name=John, age=25}
- Request Body: Form data with fields
serveStatic(); // Enable serving resources from the static folder in resources
get("/getFile/info.zip", _ -> resolvePath("/info.zip"));
- Purpose: Serve static files like images, documents, or archives.
- Example:
- Request:
/getFile/info.zip
- File Response:
info.zip
fromresources/static
.
- Request:
Happy coding! ✨