Your task is to build a new Spring feature called greeting, and connect it to the existing todo feature.
Create a REST endpoint /api/greeting that returns a message like: "Hello from Spring! You have 3 open tasks."
This feature should use the existing TodoService via constructor-based Dependency Injection.
-
Create a new package:
lv.ctco.springboottemplate.features.greeting -
Implement
GreetingService
- Inject
TodoServiceusing constructor-based DI - Use
todoService.getAllTodos()and count how many are not completed - Return a string like:
"Hello from Spring! You have X open tasks."
- Create
GreetingController
- Map it to
/api/greeting - Delegate to
GreetingService
- Follow naming conventions
- Class names must end with
Service,Controller, etc. - Use annotations like
@Service,@RestController,@RequestMapping
We've prepared tests to verify your work:
β ArchUnit tests will prevent you from structure issues
β
GreetingServiceIntegrationTest.java
π Location: src/test/java/lv/ctco/springboottemplate/features/greeting/
π Also @Disabled. Remove to test your endpoint.
If All tests pass and the greeting appears at
/api/greeting, you've completed the task!
- Constructor injection is preferred over
@Autowired - Use Java Streams to filter open todos
- The
TodoServiceis already a Spring bean β you can inject it!
When both tests are green and the endpoint works, congratulations!
You've created a feature using Spring idioms and learned how features interact via Dependency Injection.
π₯ Welcome to the backend side.