0
0
Spring Bootframework~10 mins

Business logic in services in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a service class in Spring Boot.

Spring Boot
@[1]
public class UserService {
    // business logic here
}
Drag options to blanks, or click blank then click option'
AService
BComponent
CRepository
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller instead of @Service
Forgetting to add any annotation
Using @Repository which is for data access
2fill in blank
medium

Complete the code to inject a repository into the service using Spring Boot.

Spring Boot
@Service
public class OrderService {
    private final OrderRepository orderRepository;

    public OrderService([1] orderRepository) {
        this.orderRepository = orderRepository;
    }
}
Drag options to blanks, or click blank then click option'
Afinal
BOrderRepository
C@Autowired
Dprivate
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Autowired as a parameter type
Using 'final' or 'private' as parameter type
Forgetting to specify the repository type
3fill in blank
hard

Fix the error in the method that saves an order in the service.

Spring Boot
public void saveOrder(Order order) {
    orderRepository.[1](order);
}
Drag options to blanks, or click blank then click option'
Asave
BsaveOrder
Cinsert
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like saveOrder
Using insert which is not in JpaRepository
Using add which is not a repository method
4fill in blank
hard

Fill both blanks to create a method that finds an order by ID and returns it or null if not found.

Spring Boot
public Order getOrderById(Long id) {
    return orderRepository.[1](id).[2](null);
}
Drag options to blanks, or click blank then click option'
AfindById
BorElse
Cget
DorElseGet
Attempts:
3 left
💡 Hint
Common Mistakes
Using get() without checking if present
Using orElseGet with null directly
Using non-existent repository methods
5fill in blank
hard

Fill all three blanks to create a method that updates an order's status if it exists.

Spring Boot
public boolean updateOrderStatus(Long id, String status) {
    return orderRepository.findById([1]).map(order -> {
        order.setStatus([2]);
        orderRepository.[3](order);
        return true;
    }).orElse(false);
}
Drag options to blanks, or click blank then click option'
Aid
Bstatus
Csave
Dorder
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for parameters
Forgetting to save the updated order
Returning true without updating