actix/actix-web · error · syn::Error

The #[route(..)] macro requires at least one `method`…

Error message

The #[route(..)] macro requires at least one `method` attribute

What it means

This compile-time error fires when the `#[route("/path")]` macro is used without specifying at least one HTTP method via the `method = "..."` option. Unlike the shorthand macros (`#[get(...)]`, `#[post(...)]`), the `#[route(...)]` macro does not imply a method, so `args.methods` remains empty and the check at line 358-363 fails.

Solutions

  1. Add at least one method option: `#[route("/users", method = "GET")]`
  2. Alternatively, use the shorthand `#[get("/users")]` if only one method is needed
  3. For multiple methods: `#[route("/users", method = "GET", method = "POST")]`

Example fix

// before
#[route("/users")]
async fn users() -> impl Responder { ... }

// after
#[route("/users", method = "GET")]
async fn users() -> impl Responder { ... }
Defensive patterns

Strategy: type-guard

Type guard

// Compile-time error. The #[route(...)] macro requires explicit method(s):
// #[route("/users", method = "GET")] — correct
// #[route("/users")] — incorrect (no method)
// Or use shorthand: #[get("/users")]

Prevention

When it happens

Trigger: Writing `#[route("/users")]` without any `method = "GET"` option. The `route` macro requires you to explicitly declare which HTTP method(s) it handles.

Common situations: Developers switching from `#[get("/path")]` to `#[route("/path")]` and forgetting to add the method option, or assuming `route` defaults to GET.

Related errors


AI-assisted analysis of actix/actix-web@4d435abc28 (2026-08-09). Data as JSON: /api/errors/6399bc9de79182c4. Report an issue: GitHub.

Appendix: source

Thrown at actix-web-codegen/src/route.rs:359

}

impl Route {
    pub fn new(args: RouteArgs, ast: syn::ItemFn, method: Option<MethodType>) -> syn::Result<Self> {
        let name = ast.sig.ident.clone();

        // Try and pull out the doc comments so that we can reapply them to the generated struct.
        // Note that multi line doc comments are converted to multiple doc attributes.
        let doc_attributes = ast
            .attrs
            .iter()
            .filter(|attr| attr.path().is_ident("doc"))
            .cloned()
            .collect();

        let args = Args::new(args, method)?;

        if args.methods.is_empty() {
            return Err(syn::Error::new(
                Span::call_site(),
                "The #[route(..)] macro requires at least one `method` attribute",
            ));
        }

        if matches!(ast.sig.output, syn::ReturnType::Default) {
            return Err(syn::Error::new_spanned(
                ast,
                "Function has no return type. Cannot be used as handler",
            ));
        }

        Ok(Self {
            name,
            args: vec![args],
            ast,
            doc_attributes,
        })

View on GitHub (pinned to 4d435abc28)