📖 Scenario: You are starting a new Spring Boot application for a simple book catalog. Understanding the project structure helps you organize your code and files properly.
🎯 Goal: Build the basic Spring Boot project structure with main application class, a controller package, and a simple REST controller.
📋 What You'll Learn
Create the main application class with the correct package and annotation
Add a configuration variable for the base API path
Create a REST controller class inside the controller package
Add a simple GET endpoint that returns a welcome message
💡 Why This Matters
🌍 Real World
Spring Boot is widely used to build backend services and APIs. Understanding the project structure helps you organize code for maintainability and scalability.
💼 Career
Knowing how to set up and navigate Spring Boot projects is essential for Java backend developer roles.
Progress0 / 4 steps
1
Create the main Spring Boot application class
Create a package called com.example.bookcatalog. Inside it, create a class named BookCatalogApplication annotated with @SpringBootApplication. Add the main method that runs SpringApplication.run(BookCatalogApplication.class, args).
Spring Boot
Hint
The main class must be in the base package and annotated with @SpringBootApplication. The main method starts the app.
2
Add a configuration variable for the base API path
Inside the BookCatalogApplication class, add a public static final String variable named BASE_API_PATH and set it to "/api/books".
Spring Boot
Hint
This variable will hold the base path for your API endpoints.
3
Create a REST controller class inside the controller package
Create a new package com.example.bookcatalog.controller. Inside it, create a class named BookController annotated with @RestController. Import the annotation from org.springframework.web.bind.annotation.RestController.
Spring Boot
Hint
The controller class handles HTTP requests. Use @RestController to mark it.
4
Add a simple GET endpoint that returns a welcome message
Inside BookController, add a method named welcome annotated with @GetMapping(BookCatalogApplication.BASE_API_PATH). The method returns a String with the text "Welcome to the Book Catalog!". Import @GetMapping from org.springframework.web.bind.annotation.GetMapping.
Spring Boot
Hint
This method handles GET requests at the base API path and returns a welcome message.
Practice
(1/5)
1. In a typical Spring Boot project, where should you place your main application Java files?
easy
A. src/resources/java
B. src/main/resources
C. src/test/java
D. src/main/java
Solution
Step 1: Understand Spring Boot folder roles
The src/main/java folder is for main Java source code including the application entry point.
Step 2: Identify correct folder for main Java files
Main application files should not be in resources or test folders, so src/main/java is correct.
Final Answer:
src/main/java -> Option D
Quick Check:
Main Java code = src/main/java [OK]
Hint: Main Java code always goes under src/main/java [OK]
Common Mistakes:
Confusing resources folder with Java source folder
Placing main code in test folder
Using incorrect folder names like src/resources/java
2. Which file is commonly used to manage dependencies and build steps in a Spring Boot Maven project?
easy
A. pom.xml
B. application.properties
C. build.gradle
D. settings.xml
Solution
Step 1: Identify build tool for Maven projects
Maven uses pom.xml to manage dependencies and build configuration.
Step 2: Differentiate from Gradle and config files
build.gradle is for Gradle, application.properties is for app config, and settings.xml is not for dependencies.
The src/test/java folder is for Java test source files.
Step 2: Identify test Java files location
Test Java files go under src/test/java, not in main or resources folders.
Final Answer:
src/test/java -> Option B
Quick Check:
Test Java code = src/test/java [OK]
Hint: Test Java files go in src/test/java folder [OK]
Common Mistakes:
Putting test Java files in src/main/java
Confusing test resources with test Java code
Using src/main/resources for tests
4. You accidentally placed your application.properties file inside src/main/java. What problem will this cause?
medium
A. The application will fail to start due to missing config
B. The build will fail with a syntax error
C. The properties file will be ignored and default config used
D. The application.properties will be loaded correctly anyway
Solution
Step 1: Know where Spring Boot loads config files
Spring Boot loads application.properties from src/main/resources or classpath root.
Step 2: Effect of wrong file location
If placed in src/main/java, it won't be found and ignored, so defaults apply.
Final Answer:
The properties file will be ignored and default config used -> Option C
Quick Check:
Config files must be in resources, else ignored [OK]
Hint: Config files must be in resources folder to load [OK]
Common Mistakes:
Assuming app fails to start immediately
Thinking build fails due to config file location
Believing misplaced properties still load
5. You want to add a new configuration file for your Spring Boot app that should be packaged inside the jar and accessible at runtime. Where should you place this file?
hard
A. src/main/resources/config
B. src/main/java/config
C. src/test/resources/config
D. src/main/resources/static
Solution
Step 1: Understand resource packaging in Spring Boot
Files in src/main/resources are packaged inside the jar and available at runtime.
Step 2: Choose correct folder for config files
Config files should be under src/main/resources/config to be included and organized.
Final Answer:
src/main/resources/config -> Option A
Quick Check:
Runtime config files go in src/main/resources [OK]
Hint: Put runtime config files in src/main/resources folder [OK]
Common Mistakes:
Placing config files in src/main/java which is for code
Using test resources folder which is not packaged
Putting config in static folder meant for web assets
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.