Challenge

Project 1 - Create a new microservice for allowing customers to getInsurance policy from your banking app

Creating a complete microservices project in a single response isn't feasible due to the length limitations. However, I can provide you with a basic outline and code snippets to get you started with the "Loans" microservice using Spring Boot.

  1. Set Up a Spring Boot Project:

  2. Define Entity Class:

    • Create an entity class representing the "Loans" table.
    java
    @Entity @Table(name = "loans") public class Loan { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int loanNumber; @Column(name = "customer_id") private int customerId; @Column(name = "loan_type") private String loanType; @Column(name = "loanate int loanAmount; @Column(name = "loan_end_date") private Date loanEndDate; // Getters and Setters }
  3. Create Repository Interface:

    • Create a repository interface that extends JpaRepository to handle database operations.
    java
    public interface LoanRepository extends JpaRepository { List findByCustomerId(int customerId); }
  4. Create Controller:

    • Create a controller to handle HTTP requests.
    java
    @RestController @RequestMapping("/loans") public class LoanController { @Autowired private LoanRepository loanRepository; @PostMapping public List getLoansForCustomer(@RequestBody Map request) { int customerId = request.get("customerId"); return loanRepository.findByCustomerId(customerId); } }
  5. Configure H2 Database:

    • Add the necessary properties in application.properties or application.yml to configure the H2 database.
    less
    spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=password spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
  6. Create Sample Data (data.sql):

    • Create a data.sql file in the src/main/resources directory to insert test data.
    sql
    INSERT INTO loans (customer_id, loan_type, loan_amount, loan_end_date) VALUES (1, 'HOUSING LOAN', 10000, '2023-12-31'); -- Add more sample data as needed
  7. Build and Run:

    • Build and run the application. You should now be able to access the Loans microservice.

Remember to handle exceptions, add proper error handling, and consider security measures if this application is intended for production use. Additionally, you may need to set up dependency management using Maven or Gradle, and possibly add a Dockerfile if you want to containerize the microservice.

  1. Create the Loans Microservice Project:

    • Follow the previous instructions to set up the Spring Boot project for the "Loans" microservice.
  2. Create a Dockerfile:

    • In the root of your "Loans" microservice project, create a file named Dockerfile with the following content:

To demonstrate the "Loans" microservice using Docker and Postman, we'll follow these steps:

Create the Loans Microservice Project:

Follow the previous instructions to set up the Spring Boot project for the "Loans" microservice.

Create a Dockerfile:

In the root of your "Loans" microservice project, create a file named Dockerfile with the following content:

Dockerfile

Copy code

FROM openjdk:11

ADD target/loans-microservice.jar loans-microservice.jar

EXPOSE 9500

ENTRYPOINT ["java", "-jar", "loans-microservice.jar"]

This Dockerfile specifies a base image, adds the compiled JAR file to the image, exposes port 9500, and defines the entry point to run the application.

Build the Docker Image:

Open a terminal or command prompt and navigate to the root directory of your project.

Run the following command to build the Docker image:

Copy code

docker build -t loans-microservice .

Run the Docker Container:

After the image is built successfully, you can run a Docker container using the following command:

arduino

Copy code

docker run -p 9500:9500 loans-microservice

This command maps port 9500 of the host machine to port 9500 inside the container.

Test with Postman:

Open Postman and create a new request to test the "Loans" microservice.

Set the request type to POST and enter the URL: http://localhost:9500/loans.

In the request body, select raw and choose JSON (application/json). Then, enter the request body with the customerId parameter:

Json

{

    "customerId": 1

}

Send the request.

Verify the Response:

You should receive a response containing the loan details for the queried customer ID.

Remember to replace loans-microservice.jar with the actual name of your compiled JAR file if it's different. This example assumes that your microservice is running on port 9500. Adjust the port if it's different in your application.

Please ensure Docker Desktop is installed and running on your system before executing these commands. This example assumes that your microservice is configured to work with the H2 database as per your previous instructions.

Sending a POST Request:

Open Postman and create a new request.

Set the request type to POST.

Enter the URL for your "Loans" microservice (e.g., http://localhost:9500/loans).

In the request body, select raw and choose JSON (application/json). Enter the request body with the customerId parameter.

Click the "Send" button to make the request.

Taking a Screenshot:

After you receive a response from your microservice in Postman, you can take a screenshot by using your device's screenshot functionality.

On most devices, you can press a combination of keys (e.g., PrtScn or Alt + PrtScn on Windows, Command + Shift + 3 on macOS) to take a screenshot. The exact keys may vary depending on your operating system.

Saving and Sharing the Screenshot:

Save the screenshot to a location on your device (e.g., Desktop or Pictures folder).

If you want to share the screenshot, you can use email, messaging apps, or file-sharing platforms.

Remember to ensure that the microservice is running, and you've sent a valid request using Postman before taking the screenshot.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.