DEV Community

Ritik Kumar
Ritik Kumar

Posted on

Become a 1% Developer: Create a REST API Without Any Framework (No Spring Boot, No Spring!)

If you’ve landed on this page, it probably means you’re one of those rare developers who want to dig deeper — someone who wants to understand how things really work under the hood.

I was exactly like you — curious, restless, and eager to learn how web servers actually handle requests without relying on frameworks like Spring Boot or Spring.

So, in this article, I’ll walk you through how to build a REST API endpoint from scratch in Java — no frameworks, no magic, just the raw fundamentals.

🧩 Step 1: Create a New Maven Project

Start by creating a simple Maven project. You don’t need any extra dependencies for this tutorial — Java’s built-in libraries are more than enough.

🧠 Step 2: The Minimal Code to Get Started

Here’s all the code you need to spin up a basic REST API server using pure Java:

package com.ritik;

import com.sun.net.httpserver.HttpServer;
import java.io.OutputStream;
import java.net.InetSocketAddress;

public class Main {
    static void main() {
        try {
            HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);

            // First Endpoint
            server.createContext("/api/hello", (exchange) -> {
                System.out.println("Request Came");
                String response = "Hello World!";
                exchange.sendResponseHeaders(200, response.getBytes().length);
                OutputStream outputStream = exchange.getResponseBody();
                outputStream.write(response.getBytes());
                outputStream.flush();
                exchange.close();
            });

            // Second Endpoint
            server.createContext("/api/world", (exchange) -> {
                if (exchange.getRequestMethod().equalsIgnoreCase("GET")) {
                    System.out.println("Request Came");
                    String response = "Thanks for connecting with the world!";
                    exchange.sendResponseHeaders(200, response.getBytes().length);
                    OutputStream outputStream = exchange.getResponseBody();
                    outputStream.write(response.getBytes());
                    outputStream.flush();
                } else {
                    exchange.sendResponseHeaders(404, -1);
                }
                exchange.close();
            });

            server.start();
            System.out.println("Server started on port 8080");

        } catch (Exception e) {
            System.out.println("Error Creating Server: " + e.getLocalizedMessage());
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

⚙️ Step 3: Breaking Down the Code

Let’s unpack what’s happening here.

🔹 HttpServer

This is the heart of our setup.
HttpServer is a built-in Java class that helps us create lightweight HTTP servers without needing any external dependencies.

You can think of it as a minimal version of what frameworks like Spring Boot do behind the scenes.

We create an instance using:
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);

🔹 server.createContext()

This is where the magic happens.

The createContext() method acts like a router. It maps a URL path (like /api/hello) to the logic that should run when that path is accessed.

Example:

server.createContext("/api/hello", (exchange) -> { ... });

Inside this block, you handle everything from request validation to response writing.

🔹 Sending Responses

Every time your API endpoint is called, you need to send back a response.
You do this with two main steps:

exchange.sendResponseHeaders(200, response.getBytes().length);
OutputStream outputStream = exchange.getResponseBody();
outputStream.write(response.getBytes());
outputStream.flush();
exchange.close();
Enter fullscreen mode Exit fullscreen mode

sendResponseHeaders(200, length) sets the HTTP status code (200 OK) and the response length.

getResponseBody() gives you an output stream to write data back to the client.

Always remember to flush() and close() the connection to avoid memory leaks.

🌍 Step 4: Testing the API

Once you run the program, you’ll see:

Server started on port 8080

Now open your browser or use curl to test your endpoints:

curl http://localhost:8080/api/hello

Output:

Hello World!

And for the second endpoint:

curl http://localhost:8080/api/world

Output:

Thanks for connecting with the world!
💡 Step 5: What You Just Built

You’ve just built a fully functional REST API — from scratch — without using any frameworks.
This simple code can handle multiple routes, validate request methods, and send back responses, all powered by Java’s standard library.

Here’s what’s exciting:

No Spring Boot overhead.

No hidden configuration.

You directly control request handling.

This is how you start thinking like a 1% developer — by understanding the fundamentals before relying on abstraction.

🚀 What’s Next?

Now that you’ve built a raw HTTP server:

Try adding POST and DELETE endpoints.

Parse JSON requests using com.google.gson or org.json.

Implement basic routing and middleware (like authentication).
Step by step, you’ll see how frameworks like Spring Boot evolved from these very basics.

❤️ Final Thoughts

Becoming a 1% developer isn’t about memorizing libraries — it’s about mastering the core concepts that power them.
When you know how to build an API without Spring Boot, you gain a whole new appreciation for what frameworks do for you — and more importantly, you know how to fix things when they break.

So keep experimenting. Break things. Learn how they work.
That’s the real developer mindset.

If you found this useful, give it a clap 👏 and share it with your developer friends who love to learn the real stuff.

Top comments (0)