{"id":209,"date":"2026-05-29T11:43:31","date_gmt":"2026-05-29T11:43:31","guid":{"rendered":"https:\/\/ranjeshviswa.com\/?p=209"},"modified":"2026-05-29T11:43:31","modified_gmt":"2026-05-29T11:43:31","slug":"python-based-fastapi-for-java-based-spring-boot-developers","status":"publish","type":"post","link":"https:\/\/ranjeshviswa.com\/?p=209","title":{"rendered":"Python based FastAPI for Java based Spring Boot Developers"},"content":{"rendered":"<h2>Overview<\/h2>\n<p class=\"isSelectedEnd\">FastAPI is a modern Python framework for building REST APIs and backend services. It is designed around Python type hints, asynchronous programming, and automatic API documentation generation.<\/p>\n<p class=\"isSelectedEnd\">For Java developers coming from Spring Boot, FastAPI feels lightweight, explicit, and highly productive while still supporting enterprise-grade REST services.<\/p>\n<p class=\"isSelectedEnd\">FastAPI offers:<\/p>\n<ul data-spread=\"false\">\n<li>Lightweight and fast startup<\/li>\n<li>Async support using Python asyncio<\/li>\n<li>Automatic OpenAPI and Swagger documentation<\/li>\n<li>Built-in request validation using Pydantic<\/li>\n<li>Dependency injection support<\/li>\n<li>Minimal boilerplate<\/li>\n<li>Excellent developer productivity<\/li>\n<li>Suitable for microservices and APIs<\/li>\n<\/ul>\n<h2>Introduction<\/h2>\n<p class=\"isSelectedEnd\">In this article, we will show how to create a FastAPI based REST application. The concepts will be explained from the perspective of Spring Boot developers.<\/p>\n<p class=\"isSelectedEnd\">The example project uses:<\/p>\n<ul data-spread=\"false\">\n<li>FastAPI<\/li>\n<li>SQLAlchemy ORM<\/li>\n<li>Pydantic models<\/li>\n<li>SQLite database<\/li>\n<\/ul>\n<p class=\"isSelectedEnd\">Code location:<\/p>\n<p class=\"isSelectedEnd\"><a href=\"https:\/\/github.com\/ranjesh1\/python-rest-services\/?utm_source=chatgpt.com\">python-rest-services GitHub Repository<\/a><\/p>\n<div contenteditable=\"false\">\n<hr \/>\n<\/div>\n<h1>Application Startup<\/h1>\n<p class=\"isSelectedEnd\">The following is conceptually similar to Spring Boot\u2019s <code dir=\"ltr\">@SpringBootApplication<\/code> annotated class and <code dir=\"ltr\">SpringApplication.run()<\/code>.<\/p>\n<h2>FastAPI<\/h2>\n<pre dir=\"ltr\"><code dir=\"ltr\">from fastapi import FastAPI\r\nfrom app.routes.user_routes import router as user_router\r\nfrom app.routes.order_routes import router as order_router\r\n\r\napp = FastAPI()\r\n\r\napp.include_router(user_router)\r\napp.include_router(order_router)<\/code><\/pre>\n<h2>Spring Boot Equivalent<\/h2>\n<pre dir=\"ltr\"><code dir=\"ltr\">@SpringBootApplication\r\npublic class Application {\r\n   public static void main(String[] args) {\r\n      SpringApplication.run(Application.class, args);\r\n   }\r\n}<\/code><\/pre>\n<p class=\"isSelectedEnd\">In FastAPI:<\/p>\n<ul data-spread=\"false\">\n<li><code dir=\"ltr\">FastAPI()<\/code> creates the application instance<\/li>\n<li><code dir=\"ltr\">include_router()<\/code> registers REST routes<\/li>\n<li>The framework automatically exposes Swagger UI<\/li>\n<\/ul>\n<p class=\"isSelectedEnd\">Swagger UI is available at:<\/p>\n<pre dir=\"ltr\"><code dir=\"ltr\">http:\/\/localhost:8000\/docs<\/code><\/pre>\n<p class=\"isSelectedEnd\">This is conceptually similar to Spring Boot applications using:<\/p>\n<ul data-spread=\"false\">\n<li>springdoc-openapi<\/li>\n<li>springfox swagger<\/li>\n<\/ul>\n<div contenteditable=\"false\">\n<hr \/>\n<\/div>\n<h1>Routes<\/h1>\n<p class=\"isSelectedEnd\">Routes in FastAPI are conceptually similar to Spring Boot Controllers.<\/p>\n<h2>FastAPI<\/h2>\n<pre dir=\"ltr\"><code dir=\"ltr\">from fastapi import APIRouter, HTTPException\r\nfrom typing import List\r\n\r\nrouter = APIRouter(prefix=\"\/users\", tags=<span class=\"text-token-text-primary cursor-text rounded-sm\" data-placeholder-token=\"true\">[\"Users\"]<\/span>)\r\n\r\n@router.get(\"\/\", response_model=List<span class=\"text-token-text-primary cursor-text rounded-sm\" data-placeholder-token=\"true\">[UserResponse]<\/span>)\r\ndef get_users():\r\n    return user_service.get_all_users()\r\n\r\n@router.get(\"\/{user_id}\")\r\ndef get_user(user_id: int):\r\n    user = user_service.get_user(user_id)\r\n\r\n    if not user:\r\n        raise HTTPException(status_code=404, detail=\"User not found\")\r\n\r\n    return user\r\n\r\n@router.post(\"\/\")\r\ndef create_user(user: UserCreate):\r\n    return user_service.create_user(user)<\/code><\/pre>\n<h2>Spring Boot Equivalent<\/h2>\n<pre dir=\"ltr\"><code dir=\"ltr\">@RestController\r\n@RequestMapping(\"\/users\")\r\npublic class UserController {\r\n\r\n    @GetMapping\r\n    public List&lt;User&gt; getUsers() {\r\n        return userService.getUsers();\r\n    }\r\n\r\n    @GetMapping(\"\/{id}\")\r\n    public User getUser(@PathVariable Long id) {\r\n        return userService.getUser(id);\r\n    }\r\n\r\n    @PostMapping\r\n    public User createUser(@RequestBody User user) {\r\n        return userService.createUser(user);\r\n    }\r\n}<\/code><\/pre>\n<p class=\"isSelectedEnd\">Similarities:<\/p>\n<ul data-spread=\"false\">\n<li><code dir=\"ltr\">@router.get()<\/code> is similar to <code dir=\"ltr\">@GetMapping<\/code><\/li>\n<li><code dir=\"ltr\">@router.post()<\/code> is similar to <code dir=\"ltr\">@PostMapping<\/code><\/li>\n<li>Path parameters work similarly<\/li>\n<li>JSON serialization is automatic<\/li>\n<\/ul>\n<div contenteditable=\"false\">\n<hr \/>\n<\/div>\n<h1>Request Validation<\/h1>\n<p class=\"isSelectedEnd\">FastAPI uses Pydantic models for request validation.<\/p>\n<h2>FastAPI<\/h2>\n<pre dir=\"ltr\"><code dir=\"ltr\">from pydantic import BaseModel\r\n\r\nclass UserCreate(BaseModel):\r\n    first_name: str\r\n    last_name: str\r\n    email: str<\/code><\/pre>\n<p class=\"isSelectedEnd\">This is similar to Spring Boot DTO validation.<\/p>\n<h2>Spring Boot Equivalent<\/h2>\n<pre dir=\"ltr\"><code dir=\"ltr\">public class UserRequest {\r\n    @NotBlank\r\n    private String firstName;\r\n\r\n    @NotBlank\r\n    private String lastName;\r\n\r\n    @Email\r\n    private String email;\r\n}<\/code><\/pre>\n<p class=\"isSelectedEnd\">FastAPI automatically:<\/p>\n<ul data-spread=\"false\">\n<li>validates requests<\/li>\n<li>returns validation errors<\/li>\n<li>generates API schema documentation<\/li>\n<\/ul>\n<p class=\"isSelectedEnd\">without extra configuration.<\/p>\n<div contenteditable=\"false\">\n<hr \/>\n<\/div>\n<h1>Database Models<\/h1>\n<p class=\"isSelectedEnd\">FastAPI commonly uses SQLAlchemy ORM.<\/p>\n<h2>SQLAlchemy Model<\/h2>\n<pre dir=\"ltr\"><code dir=\"ltr\">from sqlalchemy import Column, Integer, String\r\nfrom app.database import Base\r\n\r\nclass User(Base):\r\n    __tablename__ = \"users\"\r\n\r\n    id = Column(Integer, primary_key=True, index=True)\r\n    first_name = Column(String)\r\n    last_name = Column(String)\r\n    email = Column(String)<\/code><\/pre>\n<h2>Spring Boot Equivalent<\/h2>\n<pre dir=\"ltr\"><code dir=\"ltr\">@Entity\r\n@Table(name = \"users\")\r\npublic class User {\r\n    @Id\r\n    @GeneratedValue\r\n    private Long id;\r\n\r\n    private String firstName;\r\n    private String lastName;\r\n    private String email;\r\n}<\/code><\/pre>\n<div contenteditable=\"false\">\n<hr \/>\n<\/div>\n<h1>Repository Layer<\/h1>\n<p class=\"isSelectedEnd\">FastAPI applications often use SQLAlchemy sessions directly.<\/p>\n<h2>FastAPI<\/h2>\n<pre dir=\"ltr\"><code dir=\"ltr\">def get_user(db: Session, user_id: int):\r\n    return db.query(User)\\\r\n             .filter(User.id == user_id)\\\r\n             .first()<\/code><\/pre>\n<h2>Spring Boot Equivalent<\/h2>\n<pre dir=\"ltr\"><code dir=\"ltr\">public interface UserRepository\r\n    extends JpaRepository&lt;User, Long&gt; {\r\n}<\/code><\/pre>\n<p class=\"isSelectedEnd\">Spring Boot provides more abstraction through Spring Data JPA, while FastAPI typically uses SQLAlchemy queries directly.<\/p>\n<div contenteditable=\"false\">\n<hr \/>\n<\/div>\n<h1>Dependency Injection<\/h1>\n<p class=\"isSelectedEnd\">FastAPI also supports dependency injection.<\/p>\n<h2>FastAPI<\/h2>\n<pre dir=\"ltr\"><code dir=\"ltr\">from fastapi import Depends\r\n\r\ndef get_db():\r\n    db = SessionLocal()\r\n    try:\r\n        yield db\r\n    finally:\r\n        db.close()\r\n\r\n@router.get(\"\/\")\r\ndef get_users(db: Session = Depends(get_db)):\r\n    return user_service.get_users(db)<\/code><\/pre>\n<h2>Spring Boot Equivalent<\/h2>\n<pre dir=\"ltr\"><code dir=\"ltr\">@Autowired\r\nprivate UserService userService;<\/code><\/pre>\n<p class=\"isSelectedEnd\">FastAPI dependency injection is:<\/p>\n<ul data-spread=\"false\">\n<li>explicit<\/li>\n<li>function-based<\/li>\n<li>lightweight<\/li>\n<\/ul>\n<p class=\"isSelectedEnd\">compared to Spring\u2019s annotation-driven dependency injection.<\/p>\n<div contenteditable=\"false\">\n<hr \/>\n<\/div>\n<h1>Automatic API Documentation<\/h1>\n<p class=\"isSelectedEnd\">One of FastAPI\u2019s strongest features is automatic API documentation generation.<\/p>\n<p class=\"isSelectedEnd\">Available automatically:<\/p>\n<ul data-spread=\"false\">\n<li>Swagger UI<\/li>\n<li>OpenAPI specification<\/li>\n<li>ReDoc documentation<\/li>\n<\/ul>\n<p class=\"isSelectedEnd\">Endpoints:<\/p>\n<pre dir=\"ltr\"><code dir=\"ltr\">\/docs\r\n\/redoc\r\n\/openapi.json<\/code><\/pre>\n<p class=\"isSelectedEnd\">In Spring Boot, this usually requires additional libraries such as:<\/p>\n<ul data-spread=\"false\">\n<li>springdoc-openapi<\/li>\n<li>springfox<\/li>\n<\/ul>\n<div contenteditable=\"false\">\n<hr \/>\n<\/div>\n<h1>FastAPI vs Spring Boot Comparison Summary<\/h1>\n<table>\n<tbody>\n<tr>\n<th>Feature<\/th>\n<th>FastAPI<\/th>\n<th>Spring Boot<\/th>\n<\/tr>\n<tr>\n<td>Language<\/td>\n<td>Python<\/td>\n<td>Java<\/td>\n<\/tr>\n<tr>\n<td>Startup Time<\/td>\n<td>Very Fast<\/td>\n<td>Moderate<\/td>\n<\/tr>\n<tr>\n<td>Boilerplate<\/td>\n<td>Minimal<\/td>\n<td>More<\/td>\n<\/tr>\n<tr>\n<td>Async Support<\/td>\n<td>Native asyncio<\/td>\n<td>WebFlux\/Reactor<\/td>\n<\/tr>\n<tr>\n<td>Dependency Injection<\/td>\n<td>Lightweight<\/td>\n<td>Extensive<\/td>\n<\/tr>\n<tr>\n<td>API Docs<\/td>\n<td>Built-in<\/td>\n<td>Additional libraries<\/td>\n<\/tr>\n<tr>\n<td>Ecosystem<\/td>\n<td>Growing<\/td>\n<td>Extremely Mature<\/td>\n<\/tr>\n<tr>\n<td>ORM<\/td>\n<td>SQLAlchemy<\/td>\n<td>Hibernate\/JPA<\/td>\n<\/tr>\n<tr>\n<td>Enterprise Integrations<\/td>\n<td>Moderate<\/td>\n<td>Extensive<\/td>\n<\/tr>\n<tr>\n<td>Learning Curve<\/td>\n<td>Easier<\/td>\n<td>Steeper<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div contenteditable=\"false\">\n<hr \/>\n<\/div>\n<h1>When FastAPI Works Well<\/h1>\n<p class=\"isSelectedEnd\">FastAPI is an excellent choice for:<\/p>\n<ul data-spread=\"false\">\n<li>lightweight REST APIs<\/li>\n<li>AI\/ML services<\/li>\n<li>microservices<\/li>\n<li>async applications<\/li>\n<li>rapid prototyping<\/li>\n<li>Python-centric backend systems<\/li>\n<\/ul>\n<div contenteditable=\"false\">\n<hr \/>\n<\/div>\n<h1>When Spring Boot Still Shines<\/h1>\n<p class=\"isSelectedEnd\">Spring Boot continues to be a strong choice for:<\/p>\n<ul data-spread=\"false\">\n<li>large enterprise systems<\/li>\n<li>complex integrations<\/li>\n<li>highly standardized enterprise architectures<\/li>\n<li>large teams already invested in JVM ecosystem<\/li>\n<\/ul>\n<div contenteditable=\"false\">\n<hr \/>\n<\/div>\n<h1>Conclusion<\/h1>\n<p class=\"isSelectedEnd\">For Java developers exploring Python backend development, FastAPI provides a modern and productive framework with many familiar concepts from Spring Boot:<\/p>\n<ul data-spread=\"false\">\n<li>REST controllers<\/li>\n<li>dependency injection<\/li>\n<li>request validation<\/li>\n<li>ORM support<\/li>\n<li>service layers<\/li>\n<\/ul>\n<p class=\"isSelectedEnd\">At the same time, FastAPI offers:<\/p>\n<ul data-spread=\"false\">\n<li>less boilerplate<\/li>\n<li>faster development<\/li>\n<li>async-first architecture<\/li>\n<li>automatic documentation generation<\/li>\n<\/ul>\n<p>For developers already familiar with Spring Boot concepts, learning FastAPI is relatively straightforward and highly rewarding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview FastAPI is a modern Python framework for building REST APIs and backend services. It is designed around Python type hints, asynchronous programming, and automatic API documentation generation. For Java developers coming from Spring Boot, FastAPI feels lightweight, explicit, and highly productive while still supporting enterprise-grade REST services. FastAPI offers: Lightweight and fast startup Async &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ranjeshviswa.com\/?p=209\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Python based FastAPI for Java based Spring Boot Developers&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":38,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,2,26,29,8,3,4],"tags":[],"class_list":["post-209","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fastapi","category-java","category-java-framework","category-python","category-rest-api","category-restful","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/ranjeshviswa.com\/index.php?rest_route=\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ranjeshviswa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ranjeshviswa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ranjeshviswa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ranjeshviswa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=209"}],"version-history":[{"count":2,"href":"https:\/\/ranjeshviswa.com\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":211,"href":"https:\/\/ranjeshviswa.com\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions\/211"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ranjeshviswa.com\/index.php?rest_route=\/wp\/v2\/media\/38"}],"wp:attachment":[{"href":"https:\/\/ranjeshviswa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ranjeshviswa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ranjeshviswa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}