Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk5 min

Building a REST API with Java and Spring Boot: A Practical Guide

Generate a Spring Boot project with Spring Web, return JSON from a controller, test the local endpoint, and see what a minimal example leaves for later.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To build a basic JSON endpoint with Spring Boot, generate a project that includes Spring Web, add a controller method for an HTTP request, and run the application locally. This guide walks through that path, then explains what the small example does not provide: persistent storage, production safeguards, or by itself the full REST architectural style.

What you need before you start

Spring’s starter guide lists Java 17 or later and either Maven 3.5+ or Gradle 7.5+ as prerequisites. Check the compatibility requirements for the Spring Boot release you select in Initializr; those baseline versions do not guarantee that every newer release supports every older toolchain. The official guide is at Building a RESTful Web Service.

As an Amazon Associate I earn from qualifying purchases.

Use Maven or Gradle according to the conventions and workflow you already use. Both are supported by the starter guide; there is no universally better choice for this small service.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Generate a Spring Boot project

  1. Open Spring Initializr and choose a project type, language Java, and a Spring Boot release compatible with your installed Java and build tool.
  2. Set the project’s group, artifact, and package details to suit your application.
  3. Add the Spring Web dependency, then generate and extract the project.
  4. Open the project in your IDE. Keep the generated build files and application entry point; the entry point is where the example starts.

The starter guide’s project uses @SpringBootApplication on its main application class. In this example, that annotation brings together configuration, auto-configuration, and component scanning. It is a convenient starting point, not a substitute for understanding how your application is organized as it grows.

Add a controller and a JSON representation

In Spring’s approach, an annotated controller handles HTTP requests. A @RestController marks a class as a request handler whose returned values are written to the response body. When a method returns a Java object and the web configuration supports JSON, Spring serializes that object as a JSON representation.

The official greeting example separates the representation from the request-handling method. A simplified version looks like this:

public record Greeting(long id, String content) {}
@RestController
class GreetingController {
    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/greeting")
    Greeting greeting(@RequestParam(defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(), "Hello, " + name + "!");
    }
}

Here, Greeting is the response representation; the controller maps an HTTP GET request to /greeting to a Java method. The optional name query parameter supplies the greeting name, with World used when it is omitted. The counter makes requests easy to distinguish while demonstrating a changing response. Use the exact class and package names that match your generated project, and follow the official tutorial if you want its complete runnable files: Spring’s REST service guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Run the application and inspect the endpoint

Run the generated application from your IDE, or use the project’s Maven or Gradle wrapper from a terminal. For a generated Maven project, a common command is ./mvnw spring-boot:run; for a generated Gradle project, use ./gradlew bootRun. On Windows, the wrapper scripts are typically mvnw.cmd and gradlew.bat. The exact tasks can depend on the generated project and selected build configuration.

Once the application reports that it has started, request the endpoint locally:

curl "http://localhost:8080/greeting?name=Ada"

If the default server port is unchanged and the example is running, the response is JSON with the greeting’s fields, such as {"id":1,"content":"Hello, Ada!"}. The ID may differ between requests because the sample counter increments. A browser can also open the URL for this simple GET request. Spring’s starter guide includes the full run-and-check flow at spring.io/guides/gs/rest-service.

Know what the greeting example does—and does not—store

The counter-backed greeting is a teaching example, not durable domain storage. Its changing ID comes from in-memory application state; it is not a database record. A restart does not turn that counter into a persistent identifier scheme, and the example does not demonstrate concurrent-safe business workflows, validation, or error contracts.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a data-backed expansion, Spring’s broader tutorial builds an employee service with Spring Data JPA and an H2 in-memory database. That is a separate step from returning a Java object as JSON: persistence introduces a data model and repository layer, while H2 in-memory data is not a substitute for choosing and configuring production storage. See Building REST services with Spring.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

HTTP operations are not the whole REST architectural style

A service can expose resource-shaped URLs and use HTTP GET, POST, PUT, and DELETE for CRUD operations without satisfying all of REST’s architectural constraints. Spring’s broader tutorial explicitly cautions that attractive URLs, HTTP verbs, and CRUD alone are not enough. The distinction matters when clients need to navigate the API and evolve without being tightly coupled to its internal URL layout.

The broader tutorial expands the employee service with Spring HATEOAS links and resource relations, and discusses compatibility practices. Hypermedia links can let a response describe relevant next actions or related resources, rather than requiring a client to hard-code every route. Whether that depth is needed depends on the API’s goals and client contract; it should be a deliberate design decision, not a label inferred from JSON plus CRUD endpoints. Read the tutorial at Spring’s REST services tutorial.

Choose the web stack to fit the application

Spring Boot documents both servlet-based Spring MVC and reactive Spring WebFlux, along with embedded server choices including Tomcat, Jetty, and Netty. These are architectural and runtime choices, not merely interchangeable syntax. Select the stack that fits the application’s execution model, programming style, and requirements; the documentation does not establish a universal winner. Review the current options in the Spring Boot web reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Plan the work beyond a running endpoint

A local response proves that the request path and basic serialization work. Before treating an API as ready for real clients, decide how it will handle the concerns below; the starter greeting tutorial is not a complete implementation recipe for them.

  • Persistence: choose a durable data store and model the repository and transaction boundaries rather than relying on a demonstration counter.
  • Validation and errors: define accepted inputs, validation rules, status codes, and a consistent error response.
  • Security: determine authentication, authorization, and transport requirements for the API’s users and deployment environment.
  • Testing: cover controller behavior, domain logic, and persistence boundaries with tests appropriate to the application.
  • API documentation and compatibility: make the contract understandable to clients and plan how changes will be introduced without surprising them.
  • Deployment: configure the target environment, operational monitoring, and release process. Spring Boot supports executable applications, but framework capabilities do not mean every production feature is configured or secure by default; see the Spring Boot overview.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.