Level: Beginner
Time: ~60 minutes
Learning Objectives
- Understand the default project structure
- Know the role of the main class and
@SpringBootApplication - Create a simple REST controller and return JSON
- Use
application.propertiesfor basic configuration
3.1 Project Structure
After generating from Spring Initializr you typically have:
demo/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/example/demo/
│ │ │ └── DemoApplication.java ← Main class
│ │ └── resources/
│ │ ├── application.properties ← Config
│ │ ├── static/ ← CSS, JS, images
│ │ └── templates/ ← View templates (if used)
│ └── test/
│ └── java/
│ └── com/example/demo/
│ └── DemoApplicationTests.java
├── pom.xml (or build.gradle)
└── mvnw / gradlew (wrappers)
src/main/java— application codesrc/main/resources— config, static files, templatessrc/test/java— tests
3.2 The Main Class
Example:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
What @SpringBootApplication Does
It is a composition of three annotations:
@SpringBootConfiguration— marks this class as a configuration source (like@Configuration).@EnableAutoConfiguration— enables Spring Boot auto-configuration (e.g. embedded server, default MVC config).@ComponentScan— scans the package of this class and sub-packages for@Component,@Service,@Controller, etc.
So the class that has @SpringBootApplication is both the entry point and the root of component scanning.
3.3 Your First REST Controller
Create a new class in the same package (or a sub-package, e.g. controller):
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
}
@RestController— combines@Controllerand@ResponseBody: return values are written directly to the HTTP response (e.g. JSON or plain text).- **
@GetMapping("/hello")** — maps HTTP GET requests for/hello` to this method.
Run the application and open: http://localhost:8080/hello
You should see: Hello, Spring Boot!
3.4 Returning JSON
Return an object; Spring (with Jackson on the classpath) serializes it to JSON:
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
@GetMapping("/info")
public Map<String, Object> info() {
return Map.of(
"application", "Demo",
"version", "1.0",
"status", "running"
);
}
}
Request: GET http://localhost:8080/info
Response (example): {"application":"Demo","version":"1.0","status":"running"}
3.5 Using a DTO (Data Transfer Object)
For a clearer API, use a dedicated class:
package com.example.demo.dto;
public class AppInfo {
private String application;
private String version;
private String status;
public AppInfo(String application, String version, String status) {
this.application = application;
this.version = version;
this.status = status;
}
// Getters and setters (required for JSON serialization)
public String getApplication() { return application; }
public void setApplication(String application) { this.application = application; }
public String getVersion() { return version; }
public void setVersion(String version) { this.version = version; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
}
Controller:
@GetMapping("/info")
public AppInfo info() {
return new AppInfo("Demo", "1.0", "running");
}
3.6 Configuration: application.properties
In src/main/resources/application.properties you can override defaults:
# Server
server.port=8080
# Application name (used in Actuator, logging)
spring.application.name=demo
# Custom property (we can inject this later)
app.welcome.message=Welcome to our API!
Change server.port, restart, and verify the app listens on the new port.
3.7 Summary
- Main class:
@SpringBootApplication+SpringApplication.run(...). - REST endpoint:
@RestController+@GetMapping(or other@*Mapping) methods. - JSON: Return objects; Spring uses Jackson to serialize to JSON.
- Config:
application.propertiesinsrc/main/resources.
Exercises
- Add a GET endpoint
/greet?name=YourNamethat returns"Hello, YourName!". - Create a small DTO (e.g.
Productwith id, name, price) and an endpoint that returns one product as JSON. - Move the welcome message to
application.propertiesand inject it into a controller to return it from an endpoint.