This Spring Boot application provides a RESTful API template that demonstrates clean architecture principles and dependency injection. It offers a simple endpoint that returns JSON-formatted responses containing user-provided names, showcasing Spring Boot's web capabilities and proper separation of concerns.
The project implements a layered architecture with controllers, services, and models, making it an excellent starting point for building scalable Spring Boot applications. It uses Spring Boot 3.1.1 and requires Java 24, offering modern Java features and Spring framework capabilities. The application demonstrates REST API implementation, dependency injection, and proper project structure following Spring Boot best practices.
-
Controllers (@RestController)
- Handle incoming HTTP requests
- Map URLs to specific methods using @GetMapping, @PostMapping, etc.
- Convert HTTP requests/responses to/from Java objects
- Example from our code:
@RestController public class HelloController { @GetMapping("/hello/{name}") public String hello(@PathVariable String name) { return helloService.getHello(name); } }
-
Services (@Service)
- Contain business logic
- Operate between controllers and data layer
- Typically used for data processing, validation, and business rules
- Example from our code:
@Service public class HelloService { public String getHello(String name) { return new PersonModel(name).getName(); } }
-
Models
- Represent data structures
- Define object properties and behavior
- @SpringBootApplication: Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan
- @RestController: Marks a class as a REST API controller, automatically serializes responses to JSON
- @Service: Indicates that a class contains business logic
- @GetMapping: Maps HTTP GET requests to specific handler methods
- @PathVariable: Extracts values from the URI path
- @Autowired: Enables dependency injection (shown in constructor injection)
.
├── pom.xml # Maven project configuration with Spring Boot dependencies
└── src/main/java/org/template
├── Main.java # Application entry point with Spring Boot configuration
├── controller # REST API endpoint definitions
│ └── HelloController.java
├── model # Data models and business objects
│ └── PersonModel.java
└── service # Business logic implementation
└── HelloService.java
- Java Development Kit (JDK) 24 or later
- Apache Maven 3.6.0 or later
- Your preferred IDE (IntelliJ IDEA, Eclipse, or VS Code recommended)
- Clone the repository:
git clone <repository-url>
cd StructuredProject- Build the project:
mvn clean install- Run the application:
mvn spring-boot:run- Start the application using the instructions above
- Access the REST endpoint using curl or your preferred HTTP client:
curl http://localhost:8080/hello/JohnExpected response:
{"content":"John"}- Using different names:
curl http://localhost:8080/hello/Alice
curl http://localhost:8080/hello/Bob- Using with web browsers:
Simply navigate to
http://localhost:8080/hello/YourNamein your browser.
Common issues and solutions:
- Application fails to start
- Problem: Port 8080 already in use
- Solution:
# Find process using port 8080
lsof -i :8080
# Kill the process
kill -9 <PID>- Build failures
- Problem: Incorrect Java version
- Solution: Verify Java version:
java -version
# Ensure it shows version 24 or later- Debug Mode
- Enable debug logging by adding the following to
application.properties:
logging.level.org.template=DEBUG- Run with debug mode:
mvn spring-boot:run -Dspring-boot.run.arguments=--debugThe application demonstrates Spring Boot's component interaction through a simple request-response flow:
Client Request -> HelloController -> HelloService -> PersonModel
^ |
| |
+------------------JSON Response------------------+
Component Interactions:
- Client sends HTTP GET request to
/hello/{name}(handled by @GetMapping) - HelloController receives the request and extracts the name parameter (using @PathVariable)
- HelloController delegates to HelloService (injected via constructor)
- HelloService (marked with @Service) creates a PersonModel with the provided name
- HelloService formats the response
- Response is automatically converted to JSON by Spring's message converters