Setting Up IntelliJ IDEA

Setting Up IntelliJ IDEA

  1. Open IntelliJ IDEA and navigate to the Spring Boot project you just created.

    Open Project

  2. Configure the project:

    • Right-click on the project and select Open Module Settings.

    Open Module Settings

  3. Under Project Settings, ensure that your project has SDK set to 21 and language level set to 21.

    Project SDK and Language Level

  4. Under Platform Settings, ensure that your platform SDK is set to 21.

    Platform SDK

Running the Spring Boot Project

  1. Open the file application.properties and add server.port=8081 to configure the project to run on port 8081.

    Configure Port

  2. Click on the Run icon to run the project. You can see the project running on port 8081.

    Run Project

Testing the Project with Postman

  1. Create an HTTP GET request with the URL http://localhost:8081/actuator/health to test.

    • The response should show “status”: “UP” indicating that the project is running normally.

    Test with Postman

Adding the Log4j2 Library

  1. Open the build.gradle file:

    • In the dependencies section, add the Log4j2 library with the following code:
      implementation 'org.springframework.boot:spring-boot-starter-log4j2'
      
    • Add configurations:
      configurations {
          configureEach {
              exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
              exclude group: 'commons-logging', module: 'commons-logging'
          }
      }
      

    Add Log4j2

Creating a Controller

  1. Inside the com.firstcloudjourney.productsservice directory, create a folder named products.

    Create Products Folder

  2. Inside the products folder, create a directory named controllers and create a file named ProductsController.java inside the controllers directory.

    Create Controller

  3. Copy and paste the following code into ProductsController.java:

    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/api/products")
    public class ProductsController {
        private static final Logger LOG = LogManager.getLogger(ProductsController.class);
    
        @GetMapping
        public String getAllProducts() {
            LOG.info("Get all products");
            return "All products";
        }
    }
    

    Products Controller Code

  4. Run the project and test with Postman by creating an HTTP GET request with the URL http://localhost:8081/api/products.

    Test Controller

  5. Check the logs returned in the terminal when making the HTTP GET request.

    Terminal Logs