📖 Scenario: You are building a simple Spring Boot application to understand how annotations help configure and run the app without much boilerplate code.
🎯 Goal: Learn how to use key Spring Boot annotations to create a runnable application with a REST endpoint.
📋 What You'll Learn
Create a Spring Boot main application class with the correct annotation
Add a configuration variable for the server port
Create a REST controller class with a mapping annotation
Add the final annotation to enable component scanning and auto-configuration
💡 Why This Matters
🌍 Real World
Spring Boot is widely used to build web applications and microservices quickly with minimal setup.
💼 Career
Understanding annotations in Spring Boot is essential for Java developers working on backend services and enterprise applications.
Progress0 / 4 steps
1
Create the main Spring Boot application class
Create a public class called DemoApplication with a main method that calls SpringApplication.run(DemoApplication.class, args). Annotate the class with @SpringBootApplication.
Spring Boot
Hint
The @SpringBootApplication annotation marks the main class and enables auto-configuration.
2
Add a configuration property for server port
Create a String variable called serverPort and set it to "8081" inside the DemoApplication class.
Spring Boot
Hint
This variable simulates a configuration property for the server port.
3
Create a REST controller with a GET mapping
Create a public class called HelloController annotated with @RestController. Inside it, create a method sayHello that returns "Hello, Spring Boot!" and annotate it with @GetMapping("/hello").
Spring Boot
Hint
The @RestController annotation makes the class a web controller, and @GetMapping maps HTTP GET requests.
4
Add final annotation to enable auto-configuration
Ensure the DemoApplication class is annotated with @SpringBootApplication to enable component scanning and auto-configuration.
Spring Boot
Hint
This annotation is the key to Spring Boot's auto-configuration magic.
Practice
(1/5)
1. What is the main role of annotations in Spring Boot?
easy
A. They replace the need for Java classes.
B. They tell Spring Boot how to configure and run the application.
C. They are used only for styling the user interface.
D. They slow down the application startup.
Solution
Step 1: Understand what annotations do in Spring Boot
Annotations provide metadata that guides Spring Boot on how to set up and manage components automatically.
Step 2: Compare options with this understanding
Only They tell Spring Boot how to configure and run the application. correctly states that annotations tell Spring Boot how to configure and run the app. Other options are incorrect or unrelated.
Final Answer:
They tell Spring Boot how to configure and run the application. -> Option B
Quick Check:
Annotations guide configuration = B [OK]
Hint: Annotations guide setup and running in Spring Boot [OK]
Common Mistakes:
Thinking annotations replace Java classes
Assuming annotations are for UI styling
Believing annotations slow down startup
2. Which of the following is the correct way to mark a class as a Spring Boot application entry point?
easy
A. @ComponentScan
B. @EnableWebMvc
C. @SpringBootApplication
D. @RequestMapping
Solution
Step 1: Identify the annotation that marks the main Spring Boot app class
The @SpringBootApplication annotation is used to mark the main class that starts the Spring Boot app.
Step 2: Review other options for correctness
@EnableWebMvc enables web MVC but is not the main entry point; @ComponentScan scans components; @RequestMapping maps web requests. Only @SpringBootApplication is correct here.
Final Answer:
@SpringBootApplication -> Option C
Quick Check:
Main app class annotation = A [OK]
Hint: Main app class uses @SpringBootApplication [OK]
Common Mistakes:
Confusing @EnableWebMvc as main app annotation
Using @RequestMapping on main class
Thinking @ComponentScan alone starts the app
3. Given this code snippet:
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring!";
}
}
What will be the output when accessing /hello in a browser?
medium
A. The plain text 'Hello, Spring!'
B. A 404 Not Found error
C. A JSON object with message 'Hello, Spring!'
D. An HTML page with 'Hello, Spring!' inside <h1> tags
Solution
Step 1: Understand @RestController and @GetMapping behavior
@RestController returns data directly as the response body, not a view. @GetMapping maps the URL '/hello' to the method.
Step 2: Determine the response content
The method returns a String 'Hello, Spring!'. This will be sent as plain text in the HTTP response body.
Final Answer:
The plain text 'Hello, Spring!' -> Option A
Quick Check:
@RestController returns plain text = C [OK]
Hint: @RestController returns plain text or JSON, not HTML views [OK]
Common Mistakes:
Expecting JSON output without @ResponseBody
Assuming HTML page is returned
Thinking URL mapping is missing
4. Consider this Spring Boot controller code:
@RestController
public class MyController {
@GetMapping("/data")
public String getData() {
return null;
}
}
What problem will occur when accessing /data?
medium
A. The server returns HTTP 204 No Content with empty body.
B. The server returns the string 'null' as plain text.
C. The server returns HTTP 404 Not Found because mapping is missing.
D. The server returns HTTP 500 Internal Server Error due to null response.
Solution
Step 1: Analyze the method returning null in @RestController
Returning null from a controller method that expects a response body results in Spring returning HTTP 204 No Content with an empty body.
Step 2: Understand the effect on HTTP response
Spring treats null return as no content, so it responds with HTTP 204 No Content instead of an error.
Final Answer:
The server returns HTTP 204 No Content with empty body. -> Option A
Quick Check:
Null return in @RestController causes 204 No Content = B [OK]
Hint: Returning null in @RestController results in 204 No Content [OK]
Common Mistakes:
Expecting HTTP 204 No Content automatically
Thinking null returns 'null' string
Assuming 404 error due to missing mapping
5. You want to create a Spring Boot service that automatically scans and registers all components in the package com.example.app. Which annotation combination should you use on your main application class to achieve this?
hard
A. @SpringBootApplication
@EnableWebMvc(scanBasePackages = "com.example.app")
B. @SpringBootApplication
@ComponentScan("com.example.app")
C. @EnableAutoConfiguration
@ComponentScan("com.example.app")
D. @SpringBootApplication(scanBasePackages = "com.example.app")
Solution
Step 1: Understand how to specify component scanning in Spring Boot
@SpringBootApplication supports a property scanBasePackages to define packages to scan for components automatically.
Step 2: Compare options for correct usage
@SpringBootApplication(scanBasePackages = "com.example.app") uses @SpringBootApplication with scanBasePackages attribute correctly. @SpringBootApplication
@ComponentScan("com.example.app") redundantly uses @ComponentScan which is unnecessary if scanBasePackages is set. @EnableAutoConfiguration
@ComponentScan("com.example.app") misses @SpringBootApplication. @SpringBootApplication
@EnableWebMvc(scanBasePackages = "com.example.app") misuses @EnableWebMvc which does not have scanBasePackages.
Final Answer:
@SpringBootApplication(scanBasePackages = "com.example.app") -> Option D
Quick Check:
Use scanBasePackages in @SpringBootApplication = A [OK]
Hint: Use scanBasePackages in @SpringBootApplication for scanning [OK]
Common Mistakes:
Using @ComponentScan separately when scanBasePackages suffices
Confusing @EnableWebMvc with scanning packages
Omitting @SpringBootApplication annotation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.