Skip to content

Annotations and decorators

Annotations/Decorators serve to modify code behaviour in a declarative way without explicitly changing the core logic of a function or class. They often allow for additional functionality to be “applied” to a class, method, or variable. They’re all applied at runtime

Decorators in python

from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}

Annotations in typescript (nestjs)

@Post()
createItem(@Body() body: CreateItemDto) {
return this.itemsService.create(body);
}

Annotations in java

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ItemController {
@GetMapping("/items")
public List<String> getItems() {
return List.of("item1", "item2", "item3");
}
}