Skip to content

Ra is a lightweight Java web framework for creating RESTful endpoints with minimal setup. Ideal for developers who need a quick and easy setup without the overhead of larger frameworks. Note: This is a fun project that I work on in my spare time. It’s still in development and not fully tested.

Notifications You must be signed in to change notification settings

KirstenAli/RaCore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ra Framework Documentation

Explore the complete Java documentation for the Ra framework here.


🚀 Installation

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>

📚 Examples

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.


🛠 Defining an Endpoint

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

Quick Start: One-Line JSON Response

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 }

Retrieve Path Variables

get("/getPathVariables/{id}/{name}", Request::getPathVariables);
  • Purpose: Extract path variables from the URL.
  • Example:
    • Request: /getPathVariables/12/Jay
    • Response: { "param0": "12", "param1": "Jay" }

Retrieve Query Parameters

get("/getQueryParameters", Request::getQueryParams);
  • Purpose: Extract query parameters from the URL.
  • Example:
    • Request: /getQueryParameters?name=John&age=25
    • Response: { "name": "John", "age": "25" }

Add a New Person

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}

Update a Person

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}

Delete a Person

delete("/deletePerson", _ -> "Person deleted");
  • Purpose: Delete a person.
  • Output: Person deleted

📁 Upload Files

post("/uploadFile", request -> "Files Received: " + request.getUploadedFiles().size());
  • Purpose: Handle file uploads.
  • Example:
    • Request: Upload multiple files.
    • Response: Files Received: 3

📝 Submit Form Data

post("/sendForm", request -> "Form data received: " + request.getFormFields());
  • Purpose: Process form data submitted via POST.
  • Example:
    • Request Body: Form data with fields name=John and age=25.
    • Response: Form data received: {name=John, age=25}

📂 Serve Static Files

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 from resources/static.

Happy coding! ✨

About

Ra is a lightweight Java web framework for creating RESTful endpoints with minimal setup. Ideal for developers who need a quick and easy setup without the overhead of larger frameworks. Note: This is a fun project that I work on in my spare time. It’s still in development and not fully tested.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Languages