Quiz: Getting Started with Spring Boot
Test your knowledge on creating and structuring a Spring Boot project.
30 questions - Click on each question to reveal the answer.
Questions 1-10: Spring Initializr and Structure
Question 1: What is Spring Initializr?
Answer: Spring Initializr is a web tool (start.spring.io) that generates the basic structure of a Spring Boot project:
- Choice of project type (Maven/Gradle)
- Java and Spring Boot version
- Initial dependencies
- Project metadata (groupId, artifactId)
Question 2: What is the standard structure of a Spring Boot project?
Answer:
src/
├── main/
│ ├── java/
│ │ └── com/example/demo/
│ │ └── DemoApplication.java
│ └── resources/
│ ├── application.properties
│ ├── static/
│ └── templates/
└── test/
└── java/
pom.xml
Question 3: What is the purpose of the pom.xml file?
Answer: The pom.xml (Project Object Model) file defines:
- Project metadata (groupId, artifactId, version)
- Dependencies
- Maven plugins
- Build configuration
- Spring Boot parent (
spring-boot-starter-parent)
Question 4: What is spring-boot-starter-parent?
Answer: A Maven parent that provides:
- Dependency version management
- Default plugin configuration
- UTF-8 encoding
- Configured Java version
- Build profiles
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
</parent>
Question 5: What is a Spring Boot starter?
Answer: A starter is a set of pre-configured dependencies for a specific use:
| Starter | Usage |
|---|---|
spring-boot-starter-web | REST API |
spring-boot-starter-data-jpa | Database |
spring-boot-starter-security | Security |
spring-boot-starter-test | Tests |
Question 6: Where to place the application.properties file?
Answer: In src/main/resources/application.properties or application.yml.
This file contains the application configuration (port, database, etc.).
Question 7: What is the difference between application.properties and application.yml?
Answer:
# application.properties
server.port=8080
spring.datasource.url=jdbc:postgresql://localhost/db
# application.yml
server:
port: 8080
spring:
datasource:
url: jdbc:postgresql://localhost/db
YAML is more readable for hierarchical configurations.
Question 8: What is the purpose of the static/ folder?
Answer: The src/main/resources/static/ folder contains static files served directly:
- CSS
- JavaScript
- Images
- Static HTML files
Accessible via http://localhost:8080/file.css
Question 9: What is the purpose of the templates/ folder?
Answer: The src/main/resources/templates/ folder contains server-side templates:
- Thymeleaf (.html)
- FreeMarker (.ftl)
- Mustache (.mustache)
Used for web applications with server-side rendering.
Question 10: How to run a Spring Boot application?
Answer:
# With Maven
mvn spring-boot:run
# Or by compiling the JAR
mvn clean package
java -jar target/app.jar
# From the IDE
# Run the main class
Questions 11-20: Main Class and Annotations
Question 11: What does the main class contain?
Answer:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
This is the entry point that starts the Spring context.
Question 12: What does @SpringBootApplication do?
Answer: @SpringBootApplication combines three annotations:
@Configuration: configuration class@EnableAutoConfiguration: Spring Boot auto-configuration@ComponentScan: component scanning
Question 13: What is auto-configuration?
Answer: Spring Boot automatically configures beans based on:
- Present dependencies
- Defined properties
- Existing beans
Example: adding spring-boot-starter-data-jpa automatically configures a DataSource.
Question 14: What is @RestController?
Answer: Annotation combining:
@Controller: web component@ResponseBody: returns JSON/XML
@RestController
public class UserController {
@GetMapping("/users")
public List<User> getAll() {
return userService.findAll();
}
}
Question 15: What are the HTTP mapping annotations?
Answer:
| Annotation | HTTP Method |
|---|---|
@GetMapping | GET |
@PostMapping | POST |
@PutMapping | PUT |
@DeleteMapping | DELETE |
@PatchMapping | PATCH |
@RequestMapping | All (configurable) |
Question 16: How to retrieve a URL parameter?
Answer:
// Path variable: /users/123
@GetMapping("/users/{id}")
public User getUser(@PathVariable Long id) {
return userService.findById(id);
}
// Query param: /users?name=John
@GetMapping("/users")
public List<User> search(@RequestParam String name) {
return userService.findByName(name);
}
Question 17: How to retrieve the body of a request?
Answer:
@PostMapping("/users")
public User create(@RequestBody UserRequest request) {
return userService.create(request);
}
@RequestBody deserializes the JSON into a Java object.
Question 18: What are @Component, @Service, @Repository used for?
Answer: These are Spring stereotypes that mark classes as beans:
| Annotation | Usage |
|---|---|
@Component | Generic bean |
@Service | Business logic |
@Repository | Data access |
@Controller | Web layer |
All are variants of @Component.
Question 19: What are @Configuration and @Bean?
Answer:
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Configuration: configuration class@Bean: method that produces a bean
Question 20: How to inject a dependency?
Answer:
@Service
public class UserService {
private final UserRepository userRepository;
// Constructor injection (recommended)
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
}
Questions 21-30: Configuration and Repositories
Question 21: How to set the server port?
Answer:
# application.properties
server.port=9090
# application.yml
server:
port: 9090
Default: 8080
Question 22: How to use profiles?
Answer:
# application.yml (common)
spring:
profiles:
active: dev
# application-dev.yml
server:
port: 8080
# application-prod.yml
server:
port: 80
Activation: --spring.profiles.active=prod
Question 23: How to read a configuration property?
Answer:
@Value("${app.name}")
private String appName;
@Value("${app.timeout:5000}") // Default value
private int timeout;
Question 24: What is JpaRepository?
Answer: Spring Data JPA interface that provides CRUD methods:
public interface UserRepository extends JpaRepository<User, Long> {
// Inherited methods: save, findById, findAll, delete, count...
}
Question 25: How to create a JPA entity?
Answer:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
private String email;
// Getters, setters
}
Question 26: How to configure in-memory H2?
Answer:
spring:
datasource:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
h2:
console:
enabled: true # Console: /h2-console
Perfect for development and testing.
Question 27: How does Spring Data generate queries?
Answer: By method name:
List<User> findByName(String name);
// SELECT * FROM users WHERE name = ?
List<User> findByEmailContaining(String email);
// SELECT * FROM users WHERE email LIKE '%?%'
List<User> findByAgeGreaterThanOrderByNameAsc(int age);
// SELECT * FROM users WHERE age > ? ORDER BY name ASC
Question 28: How to start the application with data?
Answer:
@Component
public class DataInitializer implements CommandLineRunner {
private final UserRepository repository;
@Override
public void run(String... args) {
repository.save(new User("Admin", "admin@example.com"));
}
}
Or with src/main/resources/data.sql.
Question 29: How to see executed SQL queries?
Answer:
spring:
jpa:
show-sql: true
properties:
hibernate:
format_sql: true
logging:
level:
org.hibernate.SQL: DEBUG
org.hibernate.orm.jdbc.bind: TRACE
Question 30: How to create an executable JAR?
Answer:
mvn clean package
# Result: target/app-0.0.1-SNAPSHOT.jar
java -jar target/app-0.0.1-SNAPSHOT.jar
The JAR contains the application and an embedded Tomcat server.
Score
| Score | Level |
|---|---|
| 27-30 | Excellent - Fundamentals mastered |
| 21-26 | Good - Some revisions needed |
| 15-20 | Average - Review the chapters |
| Less than 15 | Needs review |