For a recent HNG Stage-0 backend assignment, I developed a streamlined Spring Boot service designed for quick deployment and review. The primary objective was to create a functional microservice with a minimal footprint, making its code easily auditable and runnable.

Core Functionality:

This microservice exposes a single GET endpoint: /me. When accessed, it returns a comprehensive JSON payload. This response includes a status indicator, a concise user profile (featuring name, role, and relevant links), a current timestamp, and an amusing cat fact dynamically fetched from the `https://catfact.ninja/fact` external API.

Architectural Approach:

The service’s design emphasizes clarity and separation of concerns:

  • ProfileController: This component is responsible for handling incoming requests to the /me endpoint and orchestrating the response, returning a ProfileResponse Data Transfer Object.
  • ProfileService: The core logic resides here. It’s tasked with constructing the user profile data and securely fetching the cat fact from the external API using Java’s built-in HttpClient.
  • Profile (Domain) and ProfileResponse (DTO): These plain old Java objects define the structure of our data, facilitating clean data modeling and efficient serialization.
  • Spring Boot Entrypoint: The standard entry for launching the application, complemented by a basic unit test to confirm successful application context loading.

Design Philosophy: Why Keep it Small?

The choice for a compact design was deliberate:

  • Reduced Complexity: A smaller codebase means reviewers can quickly grasp the domain logic, controller interactions, and service responsibilities.
  • Isolated External Calls: By confining the external cat fact API call to the ProfileService, it becomes straightforward to mock for testing or to replace with a different data source if requirements change.
  • Ideal for Iteration: A focused, minimal structure is perfectly suited for early-stage development tasks, enabling rapid iteration and targeted feedback.

Getting Started Locally:

To run this service on your local machine, navigate to the project root in your terminal and execute the following command using the included Maven wrapper:

./mvnw spring-boot:run

Once the application is running, you can access the endpoint in your browser or via a tool like Postman:

`http://localhost:8080/me`

Lessons Learned and Future Enhancements:

This project offered valuable insights and highlighted areas for further development:

  • Service Focus: Reinforcing the importance of keeping services small and highly focused accelerates review cycles and improves maintainability.
  • Dependency Injection: Injecting HttpClient (or a dedicated wrapper) is crucial for making external API calls easily testable and mockable.
  • Robustness: Implementing structured error handling and comprehensive logging is essential for building resilient production-ready applications.
  • Testing: Expanding unit tests to mock external APIs and adding integration tests for controller endpoints will significantly enhance code quality and reliability.

For a deeper dive into the code, you can explore the repository here.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed