Rapina Rapina /Tutorial
Read the docs

Your First Route

Every Rapina handler is an async function annotated with a route macro. Here's the simplest one:

#[public]
#[get("/")]
async fn hello() -> &'static str {
    "Hello, Rapina!"
}

The #[public] attribute makes this endpoint accessible without authentication — by default, all Rapina routes require JWT auth. The #[get("/")] macro registers it as a GET route at the root path.

Assignment

Modify the code to:

  1. Change the route path to /hello
  2. Create a response struct with Serialize and JsonSchema derives
  3. Return a Json<T> response with a name field set to "World"

You'll need a struct like HelloResponse with a name: String field, and the handler should return Json(HelloResponse { ... }).

Show answer
use rapina::prelude::*;

#[derive(Serialize, JsonSchema)]
struct HelloResponse {
    name: String,
}

#[public]
#[get("/hello")]
async fn hello() -> Json<HelloResponse> {
    Json(HelloResponse {
        name: "World".into(),
    })
}
Tests
All tests passing — nice work!
main.rs
Response Preview
Complete the tests to see the response preview.
Previous 1 / 6 Next