CodeGym /Courses /Module 5. Spring /Developing your first Spring application

Developing your first Spring application

Module 5. Spring
Level 1, Lesson 6
Available

Before we start, let's clear one thing up: Hello World isn't just tradition — it's kind of a rite of passage. If you haven't written "Hello World" on your new framework, you haven't really started using it. Today we'll create our first Spring Boot project. And you know what? It's gonna be super simple, no unnecessary headaches. Ready? Let's go!


Step 1: Project setup

Let's start by creating the project structure. We'll use Spring Initializr — a handy tool to generate the basic skeleton of a Spring Boot app.

  1. Open Spring Initializr in your browser.
  2. Fill in the following options:
    • Project: Maven (or Gradle — your call, but we're using Maven in this example).
    • Language: Java.
    • Spring Boot Version: 3.1.0 (the latest stable version).
    • Group: com.example.
    • Artifact: helloworld.
    • Name: helloworld.
    • Packaging: jar.
    • Java Version: 17 (or another one if your environment requires it, but preferably 17+).
  3. Add the dependency:
    • Spring Web (needed for REST controllers to work).
  4. Click "Generate" — your project will be downloaded as an archive.

Unzip the project into a convenient folder on your machine, then open it in your favorite IDE. Up to now most of you used IntelliJ IDEA (Community Edition works great). It's a good fit here too.


Step 2: Project structure

Let's take a look at the project structure. It should look roughly like this:

helloworld/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/example/helloworld/
│   │   │       └── HelloworldApplication.java
│   │   ├── resources/
│   │       └── application.properties
├── pom.xml
  • src/main/java — this is where we'll write our Java code.
  • src/main/resources/application.properties — the main configuration file for the app.
  • HelloworldApplication.java — our main class annotated with @SpringBootApplication. This is where everything starts.

And of course pom.xml (or build.gradle if you picked Gradle) for dependency management.


Step 3: Creating the Hello World controller

Alright, let's do it! We need to create a controller class that returns the message "Hello World!". A controller is the component that handles HTTP requests coming to your app.

Create a new file HelloController.java in src/main/java/com/example/helloworld:


package com.example.helloworld;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

// The @RestController annotation makes this class a REST controller.
@RestController
@RequestMapping("/api") // Base path for all endpoints in this controller.
public class HelloController {

    // The @GetMapping annotation marks this method to handle GET requests.
    @GetMapping("/hello")
    public String sayHello() {
        // Return a simple message.
        return "Hello, World!";
    }
}

Quick notes:

  • @RestController combines @Controller and @ResponseBody. This is used to return data directly in the HTTP response (no templates involved).
  • @RequestMapping sets the base URL for all requests in this controller. In our case that's /api.
  • @GetMapping("/hello") means the sayHello method will be called when a client sends a GET request to /api/hello.

Step 4: Configure and run the app

Now let's make sure the app runs. Open HelloworldApplication.java (it was already generated by Spring Initializr). You'll see the following code:


package com.example.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloworldApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloworldApplication.class, args);
    }
}

This class does three things:

  1. The @SpringBootApplication annotation includes:
    • @Configuration — lets you define configuration beans.
    • @EnableAutoConfiguration — auto-configures whatever it can.
    • @ComponentScan — scans for components (beans) in the current and nested packages.
  2. The main method starts the app. Yep, Spring Boot is that simple!
  3. SpringApplication.run() initializes our context and the server.

Now run the app. You can do it:

  • In IntelliJ IDEA: click the green triangle next to main.
  • Via terminal: run mvn spring-boot:run.

If everything went fine, you'll see something like this in the terminal:


Tomcat started on port(s): 8080 (http) with context path ''
Started HelloworldApplication in 2.345 seconds

Step 5: Verify the app

Now let's check that everything works. Open your browser (or Postman, if you wanna look professional) and go to:


http://localhost:8080/api/hello

Voilà! You should see:


Hello, World!

How does it work?

When you send a GET request to /api/hello, Spring Boot automatically calls the sayHello() method in your controller. That method returns the string "Hello, World!", which gets displayed in your browser.


Common errors and how to fix them

  1. 404 (Not Found): check that the URL matches /api/hello. Maybe you typed it wrong or forgot to include /api in the @RequestMapping.
  2. The app won't start: make sure the HelloworldApplication class is located in the root package com.example.helloworld.
  3. Port error (Port already in use): means port 8080 is already taken. You can change the port in application.properties by adding:
    
    server.port=8081
    
  4. Dependency issues: make sure your pom.xml has the Spring Web dependency:
    
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    

Practical use of this knowledge

Understanding how to create a simple app is the first step toward building a full REST API. On interviews they often ask "How do you create a basic Spring Boot project?", and knowing how to set up a controller to handle HTTP requests already gives you more confidence.

Soon we'll add more advanced controllers, databases, transactions, and security, but right now you've covered the fundamental tools you'll need for more serious stuff.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION