Open In App

How to Run Your First Spring Boot Application in Eclipse IDE?

Last Updated : 16 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework and making a production-ready application in it takes very little time. Following are some of the features of Spring Boot:

  • It allows avoiding heavy configuration of XML which is present in spring
  • It provides easy maintenance and creation of REST endpoints
  • It includes embedded Tomcat-server
  • Deployment is very easy, war and jar files can be easily deployed in the tomcat server

For more information please refer to this article: Introduction to Spring Boot. So in the article Create and Setup Spring Boot Project in Eclipse IDE we have explained how to create a simple spring boot project. In this article, we are going to create a simple “Hello World” application and run it inside our Eclipse IDE.

Procedure

  1. Create and set up Spring Boot Project in IDE.
  2. Add the spring-web dependency in your pom.xml file.
  3. Create one package and name the package as “controller in the project.
  4. Run the Spring Boot application.

Step 1: Create and Setup Spring Boot Project in Eclipse IDE

You may refer to this article Create and Setup Spring Boot Project in Eclipse IDE and create your first Spring Boot Application in Eclipse IDE. 

Step 2: Add the spring-web dependency in your pom.xml file. Go to the pom.xml file inside your project and add the following spring-web dependency.

XML




<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>


 
Step 3: In your project create one package and name the package as “controller”. In the controller, the package creates a class and name it as DemoController.

Example 1A 

Java




// Java Program to Illustrate DemoController
 
// Importing package to code module
package com.example.demo.controller;
// Importing required classes
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
// Annotation
@Controller
 
// Class
public class DemoController {
 
    @RequestMapping("/hello")
    @ResponseBody
 
    // Method
    public String helloWorld()
    {
 
        // Print statement
        return "Hello World!";
    }
}


 
We have used the below annotations in our controller layer. Here in this example, the URI path is /hello.

  • @Controller: This is used to specify the controller.
  • @RequestMapping: This is used to map to the Spring MVC controller method.
  • @ResponseBody: Used to bind the HTTP response body with a domain object in the return type.

Now, our controller is ready. Let’s run our application inside the DemoApplication.java file. There is no need to change anything inside the DemoApplication.java file. 

Example 1B 

Java




// Java Program to Illustrate DemoApplication
 
// Importing package module to code
package com.example.demo;
// Importing required classes
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
// Annotation
@SpringBootApplication
 
// Main class
public class DemoApplication {
 
    // Main driver method
    public static void main(String[] args)
    {
        SpringApplication.run(DemoApplication.class, args);
    }
}


 
Step 4: Run the Spring Boot Application

To run the application click on the green icon as seen in the below image.  

After successfully running the application you can see the console as shown in the below image. Your Tomcat server started on port 8989

Try this Tomcat URL, which is running on http://localhost:8989/hello

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads