“Programming is like creating a recipe — you gather your ingredients, follow a few steps, and voilà, you have something delicious!”
Below is your step-by-step guide to spin up your very first Spring Boot service. Even if your coding experience is on par with a curious monkey, by the end you’ll have a running app, a REST endpoint, and a sense of “Hey, I’m a Java backend wizard!”
🚩 Prerequisites
- Java Development Kit (JDK) 17+
Make surejava -version
returns at least 17. - Maven or Gradle
We’ll show both setups—pick your poison. - An IDE
IntelliJ IDEA Community / Eclipse / VS Code with Java extensions — whatever floats your boat.
1️⃣ Bootstrap with Spring Initializr
Head to https://start.spring.io or use your IDE’s built-in support.
- Project: Maven or Gradle
- Language: Java
- Spring Boot: Latest (e.g. 3.2.x)
- Group:
com.example
- Artifact:
demo
- Packaging:
jar
- Java:
17
Dependencies:
- Spring Web
- (Optional) Spring DevTools — hot reload joy!
Click Generate, unzip the project, and open it in your IDE.
2️⃣ Explore the Generated Structure
pgsqlCopyEditdemo/
├── mvnw* ← wrapper scripts (Maven)
├── mvnw.cmd
├── pom.xml ← Maven descriptor
├── src/
│ ├── main/
│ │ ├── java/com/example/demo/
│ │ │ └── DemoApplication.java
│ │ └── resources/
│ │ └── application.properties
│ └── test/
│ └── java/com/example/demo/
└── build.gradle (if Gradle)
Key file:
javaCopyEdit// DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // ← Magic annotation
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
What’s @SpringBootApplication?
It’s shorthand for:
@Configuration
@EnableAutoConfiguration
@ComponentScan
3️⃣ Add Your First REST Endpoint
Let’s create a simple “Hello, World” API in five lines:
javaCopyEdit// src/main/java/com/example/demo/HelloController.java
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController // ← Makes this a REST controller
public class HelloController {
@GetMapping("/hello") // ← HTTP GET at /hello
public String hello() {
return "🐵 Hello, Spring Boot World!";
}
}
✔️ Tip: Keep controllers skinny—delegate heavy lifting to services!
4️⃣ Build & Run
With Maven
bashCopyEdit# build
./mvnw clean package
# run
./mvnw spring-boot:run
With Gradle
bashCopyEdit# build
./gradlew clean build
# run
./gradlew bootRun
After startup, you should see logs ending with:
arduinoCopyEditStarted DemoApplication in 3.45 seconds (JVM running for 4.0)
5️⃣ Test Your Endpoint
Open your browser or curl:
bashCopyEditcurl http://localhost:8080/hello
# → 🐵 Hello, Spring Boot World!
Congratulations — your first Spring Boot service is live! 🎉
6️⃣ Configuration & Next Steps
- application.properties
Customize your server port, logging, DB connections, etc. propertiesCopyEditserver.port=9090 spring.application.name=MyFirstApp
- Spring DevTools
Add dependency for auto-restart on code changes. - Packaging
Build a fat JAR (Uber-jar) that runs anywhere withjava -jar
. - Docker
Create aDockerfile
for containerized deployments. - Tests
Write unit and integration tests undersrc/test/java
.
🙊 Monkey-Proof Best Practices
- Use DTOs for request/response—don’t expose entities.
- Organize packages: CopyEdit
com.example.demo ├── controller ├── service ├── model └── repository
- Profile-Driven Config:
application-dev.properties
,application-prod.properties
. - Health Checks via Actuator: add
spring-boot-starter-actuator
.
🏁 Challenge
- Extend the
/hello
endpoint to accept aname
parameter: javaCopyEdit@GetMapping("/hello/{name}") public String hello(@PathVariable String name) { return "Hello, " + name + "!"; }
- Deploy to Heroku or Railway.app—spin up a free dyno and push your code!
- Bonus: Add Swagger UI (
springdoc-openapi-ui
) for auto-generated API docs.
👏 Ready to roll? You just nailed the foundation of Spring Boot. Next up: building real-world services, hooking up databases, and scaling like a pro. Stay tuned for more deep dives!