How to Create a Spring Boot Project in NetBeans

In this tutorial, we will walk through the process of creating a Spring Boot project using NetBeans IDE. We will cover setting up NetBeans, creating a new Spring Boot project using Spring Initializr, and running your first Spring Boot application.

Prerequisites

Before we start, ensure you have the following:

  • Java Development Kit (JDK) installed
  • NetBeans IDE installed
  • Maven installed (optional, but recommended)

Step 1: Setting Up NetBeans

  1. Download and Install NetBeans:

    • Download NetBeans from the official website.
    • Follow the installation instructions for your operating system.
  2. Install JDK:

    • Ensure you have the JDK installed. You can download it from the Oracle website or use OpenJDK.

Step 2: Creating a New Spring Boot Project

  1. Open NetBeans:

    • Launch NetBeans from your applications menu.
  2. Open Spring Initializr:

    • Go to File -> New Project.
    • In the New Project dialog, select Maven under Categories, and then select Project with Existing POM under Projects.
    • Click Next.
  3. Configure Spring Initializr:

    • Group Id: Enter your group ID (e.g., com.example).
    • Artifact Id: Enter your artifact ID (e.g., spring-boot-demo).
    • Version: Enter the version of your project (e.g., 0.0.1-SNAPSHOT).
    • Name: Enter the name of your project (e.g., spring-boot-demo).
    • Description: Enter a description for your project (e.g., Demo project for Spring Boot).
    • Package Name: Enter the package name (e.g., com.example.demo).
    • Packaging: Choose Jar.
    • Java Version: Ensure you select the appropriate Java version (e.g., 17 if you are using JDK 17).
    • Click Next.
  4. Select Dependencies:

    • On the Dependencies screen, select the dependencies you need. For a basic Spring Boot application, you can start with:
      • Spring Web
    • Click Finish.

NetBeans will generate a new Spring Boot project with the specified configurations and dependencies.

Step 3: Exploring the Project Structure

After creating the project, you will see the following structure in your NetBeans Project Explorer:

spring-boot-demo
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── demo
│   │   │               └── SpringBootDemoApplication.java
│   │   └── resources
│   │       ├── application.properties
│   └── test
│       └── java
│           └── com
│               └── example
│                   └── demo
│                       └── SpringBootDemoApplicationTests.java
└── pom.xml

Key Files and Directories

  • src/main/java: Contains your main application code.
  • src/main/resources: Contains configuration files (e.g., application.properties).
  • src/test/java: Contains your test code.
  • pom.xml: Maven configuration file.

Step 4: Writing a Simple REST Controller

  1. Create a Controller:
    • In the src/main/java/com/example/demo directory, create a new package named controller.
    • In the controller package, create a new Java class named HelloController.

HelloController.java

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}

Step 5: Running the Application

  1. Build the Project:

    • Right-click on the project root directory (spring-boot-demo) and select Clean and Build.
  2. Run the Application:

    • Right-click on the SpringBootDemoApplication class in the src/main/java/com/example/demo directory.
    • Select Run.

NetBeans will start the Spring Boot application, and you should see output in the Output tab indicating that the application has started successfully.

Step 6: Testing the Application

  1. Open a Web Browser:
    • Open your web browser and navigate to http://localhost:8080/api/hello.

You should see the response Hello, World! from your Spring Boot application.

Conclusion

In this tutorial, we have walked through the process of creating a Spring Boot project in NetBeans IDE. We used Spring Initializr to generate a new Spring Boot project, added a simple REST controller, and ran the application. By following these steps, you should now have a basic Spring Boot REST API application running in NetBeans. This setup provides a solid foundation for developing more complex Spring Boot applications.


Comments