0% found this document useful (0 votes)
79 views4 pages

Spring Boot Coding Challenges Overview

The document outlines a series of Spring Boot coding challenges categorized by difficulty: easy, medium, and hard. Each challenge includes a brief description, hints for implementation, and suggests using various Spring features such as REST controllers, JPA, Actuator, and validation annotations. The challenges cover a range of topics including APIs for meter readings, customer data, billing systems, and security implementations.

Uploaded by

msrbharath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views4 pages

Spring Boot Coding Challenges Overview

The document outlines a series of Spring Boot coding challenges categorized by difficulty: easy, medium, and hard. Each challenge includes a brief description, hints for implementation, and suggests using various Spring features such as REST controllers, JPA, Actuator, and validation annotations. The challenges cover a range of topics including APIs for meter readings, customer data, billing systems, and security implementations.

Uploaded by

msrbharath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Spring Boot Coding Challenges - With Hints

Easy - Basic Meter Reading API

Challenge: Basic Meter Reading API

Create REST endpoints to add and retrieve energy meter readings. Validate input (non-null, correct format). Use

in-memory or H2 database.

Hint: Use @RestController, @PostMapping and Spring Data JPA for in-memory storage. Use H2 and add validation

annotations.

Easy - Simple Customer API

Challenge: Simple Customer API

Build a CRUD API for customer data. Use Spring Data JPA. Add input validation for required fields like name and email.

Hint: Create a Customer entity, use Spring Data JPA repositories, and add input validation using [Link].

Easy - Spring Boot Health Check

Challenge: Spring Boot Health Check

Create a simple /health endpoint that returns the status of your service. Use Spring Actuator.

Hint: Use Spring Boot Actuator to expose /actuator/health endpoint. Customize if needed.

Easy - DTO Validation

Challenge: DTO Validation

Add validation annotations to your request DTOs. Create a global exception handler using @ControllerAdvice.

Hint: Use @Valid and [Link] annotations. Add @ControllerAdvice for uniform error handling.

Easy - Simple Scheduler

Challenge: Simple Scheduler

Schedule a job that logs a message every 10 seconds using Spring's @Scheduled annotation.

Hint: Use @Scheduled(cron = ...) in a @Component class. Enable scheduling in your config.

Easy - Hello World REST App

Challenge: Hello World REST App

Create a basic Spring Boot REST API that returns a welcome message.
Spring Boot Coding Challenges - With Hints

Hint: Use @RestController with a GET endpoint returning a string.

Medium - Customer Billing Engine

Challenge: Customer Billing Engine

Design and implement billing logic using different strategies. Each strategy should calculate based on readings. Use

Strategy Pattern.

Hint: Apply Strategy pattern for billing logic. Inject strategy into the service layer. Write unit tests for each strategy.

Medium - Feature Toggle System

Challenge: Feature Toggle System

Build APIs to enable/disable features per user. Store toggles in DB. Add caching.

Hint: Store toggles in DB and cache them using Caffeine/Redis. Create a filter or AOP interceptor for toggle evaluation.

Medium - Meter Reading Validation

Challenge: Meter Reading Validation

Write a custom validator to check for invalid meter reading data (e.g., future timestamps).

Hint: Create a custom annotation and implement ConstraintValidator interface. Handle exceptions globally.

Medium - Custom Exception Handling

Challenge: Custom Exception Handling

Use @ControllerAdvice to return structured error responses for various domain and validation errors.

Hint: Use @ControllerAdvice and @ExceptionHandler. Return ErrorResponse DTO with HTTP status.

Medium - REST API with Pagination and Sorting

Challenge: REST API with Pagination and Sorting

Create a pageable/sortable endpoint for meter readings or customers.

Hint: Use Pageable in controller method and return Page<T>. Configure default sort and size.

Medium - Upload and Parse CSV

Challenge: Upload and Parse CSV

Allow uploading meter readings in CSV format. Parse and store them.
Spring Boot Coding Challenges - With Hints

Hint: Use MultipartFile, parse CSV using Apache Commons CSV or OpenCSV, and store readings in DB.

Hard - Multi-Tenant Billing System

Challenge: Multi-Tenant Billing System

Design a multi-tenant energy billing platform supporting multiple suppliers. Ensure tenant isolation using discriminator or

schemas.

Hint: Use a discriminator column or schema-per-tenant. Load tenant context per request and isolate data.

Hard - Kafka Consumer with Dead Letter Queue

Challenge: Kafka Consumer with Dead Letter Queue

Consume meter readings from Kafka. On processing failure, redirect message to DLQ topic. Expose DLQ content via

REST.

Hint: Use Spring Kafka with error handler. On failure, send message to DLQ topic. Use @KafkaListener.

Hard - Dynamic Strategy Loader

Challenge: Dynamic Strategy Loader

Dynamically select and load billing strategies based on supplier configuration. Use Factory or Strategy pattern.

Hint: Create a factory that returns the correct BillingStrategy based on plan name or config.

Hard - Task Scheduler with Retry and Idempotency

Challenge: Task Scheduler with Retry and Idempotency

Run billing tasks on a schedule. Ensure retry on failure and idempotency.

Hint: Use Spring Scheduler with retry logic. Store job status to ensure idempotency.

Hard - Role-Based Access Control

Challenge: Role-Based Access Control

Secure your APIs using Spring Security. Implement roles like ADMIN and USER with different access rights.

Hint: Use Spring Security with method-level security. Assign roles and protect APIs accordingly.

Hard - Testcontainers Integration Test

Challenge: Testcontainers Integration Test


Spring Boot Coding Challenges - With Hints

Write an integration test using Testcontainers with a real PostgreSQL or Kafka instance.

Hint: Write integration tests using JUnit and Testcontainers for real DB/Kafka. Use @DynamicPropertySource.

Common questions

Powered by AI

Spring Boot Actuator provides a convenient way to create health check endpoints, which are crucial for monitoring the health and performance of applications. The /actuator/health endpoint offers insights into application health through predefined metrics, such as application status and memory usage. Benefits include ease of integration, comprehensive monitoring, and immediate access to health data. However, challenges may arise in customizing the health check logic to suit specific application needs and managing security configurations to safeguard sensitive information exposed through these endpoints .

A multi-tenant architecture in a billing system offers several advantages, including optimized resource usage and reduced hosting costs by serving multiple clients from a single instance. Tenant isolation can be achieved using Spring Boot by employing strategies such as discriminator columns or separate schemas per tenant. These methods ensure data segregation and security across tenants. A discriminator column distinguishes between tenants within a shared table, while schema-per-tenant creates distinct schemas for each tenant, thereby ensuring data is isolated at the database level. Tenant context loading per request is essential to maintain correct tenant data access .

To ensure data accuracy for meter readings, creating a custom validator in Spring Boot can effectively enforce additional constraints like checking against future timestamps. This is done by defining a custom annotation and implementing the ConstraintValidator interface to apply specific logic. Spring Boot supports this approach by allowing Global Exception Handling via @ControllerAdvice, which ensures consistent error responses. This method provides flexibility in data validation, ensuring that specific business logic can be embedded within the validation process .

Implementing a dead letter queue (DLQ) in Kafka consumers enhances reliability by ensuring unprocessable messages are redirected to a DLQ topic, preventing processing disruptions and data loss. This mechanism allows developers to address failed messages without affecting the primary flow. REST exposure of DLQ allows for monitoring and managing these messages, which can aid in diagnosing issues. Best practices include providing clear endpoints for DLQ retrieval, securing these endpoints with proper authentication, and using paginated data retrieval to handle large volumes of messages efficiently .

In implementing a customer billing engine using Spring Boot, strategy patterns such as the Factory or Strategy Pattern can be utilized. These patterns allow different billing logics to be encapsulated within separate classes, which can be selected and executed at runtime based on specific criteria, such as customer type or billing plan. This design promotes system flexibility and scalability, as new billing strategies can be added without modifying the existing code base. Dependency injection is used to select the appropriate strategy, enhancing the maintainability and extendability of the billing engine .

Designing a role-based access control system using Spring Security involves critical considerations, such as defining roles like ADMIN and USER with specific access rights to APIs. Method-level security annotations, such as @PreAuthorize, can restrict access based on roles. Considerations include ensuring that roles are defined based on least privilege, regularly updated to meet evolving security requirements, and managed efficiently to prevent unauthorized access. Thorough testing and regular auditing are essential to maintain API security and integrity .

Spring Boot's @Scheduled annotation simplifies the creation of scheduled tasks by allowing methods to be executed at set intervals using cron expressions. It supports simple intervals, fixed delays, and cron-style expressions for scheduling. However, @Scheduled does not inherently support retry mechanisms or ensure idempotency. Handling retries and maintaining idempotency require additional logic, such as custom retry policies and job state tracking, to prevent issues like the duplication of tasks if the system fails partway through the execution .

Spring Boot facilitates testing with Testcontainers by providing an environment to run Docker containers as part of the JUnit test lifecycle, simulating real-world dependencies like databases or message queues. This approach is crucial for microservices integration testing as it allows developers to test the interaction and behavior of different components in an environment that closely resembles production. Utilizing Testcontainers helps validate system resilience against different database states and message flows, ensuring higher test reliability and system robustness .

To effectively create a REST API in Spring Boot that handles CRUD operations for customer data, several key components must be utilized: a Customer entity for the representation of customer information, Spring Data JPA for simplifying database interactions, and various annotations for defining API functionalities. Input validations are added using javax.validation annotations to ensure data integrity, particularly for fields like name and email. Spring Data JPA repositories automate data handling tasks, and the @RestController annotation marks the class as a controller where every method returns a domain object instead of a view. @PostMapping, @GetMapping, @PutMapping, and @DeleteMapping annotations are used to define the different CRUD endpoints .

Implementing a feature toggle system in a microservices environment allows for features to be enabled or disabled at runtime without deploying new code, providing significant operational flexibility and continuous deployment capabilities. Caching can greatly enhance the performance of a feature toggle system by storing toggle states in memory, reducing database load, and speeding up feature state retrieval. Technologies such as Caffeine or Redis can be used for caching, which helps in quickly evaluating feature states and enabling seamless user experiences while minimizing latency and resource consumption .

You might also like