# Defining Your First Route
Routing in Actix Web is the process of mapping incoming HTTP requests to specific async functions (handlers). You can define routes in a few different ways, but the most common are using macros or explicitly calling routing methods.
##Macro-based Routing
Actix Web provides convenient attribute macros like #[get("/")], #[post("/user")], etc. These macros abstract away the manual registration process, making your code clean and readable.
use actix_web::{get, HttpResponse, Responder};
#[get("/hello")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello!")
}To register a macro-based route, you use the .service() method on your App:
App::new().service(hello)##Explicit Routing
Sometimes you might prefer or need explicit routing, especially if you want to apply specific middleware to a route or generate routes dynamically. You use the .route() method for this:
use actix_web::{web, App, HttpResponse, Responder};
async fn goodbye() -> impl Responder {
HttpResponse::Ok().body("Goodbye!")
}
// Inside your HttpServer factory:
App::new().route("/bye", web::get().to(goodbye))##Path Parameters
Routes aren't just static strings. They can contain dynamic parameters that you can extract. Using curly braces {} in the route path signifies a dynamic segment.
#[get("/users/{user_id}")]
async fn get_user(path: actix_web::web::Path<u32>) -> impl Responder {
let user_id = path.into_inner();
format!("Fetching user with ID: {}", user_id)
}This flexibility lets you design RESTful APIs efficiently and elegantly.