Categories: JavaWeb

Springboot Web Hello World with a Java & Pom.xml file

This blog presents instructions on how to quickly get started with Springboot Hello World with just one Java file and a Pom.xml.
Before getting set up with files, make sure you have installed and configured following
  • Java; Setup Java_Home as it is required by maven
  • Maven; Maven would be used to package our app.

 

Java Code Sample

Following is the code for Java file. Make sure to create hello/HelloController.java file within src/main/java. This is required for maven to package it correctly.

package hello;

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

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@SpringBootApplication
public class HelloController {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(HelloController.class, args);
    }

}

 

Code Sample for POM.xml

Following is the code for pom.xml. Make sure to put pom.xml at the topmost level (along side src folder)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>hello</groupId>
    <artifactId>SpringBootSampleWeb</artifactId>
    <version>0.0.1</version>
 <properties>
     <!-- The main class to start by executing java -jar -->
     <start-class>hello.HelloController</start-class>
 </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>

    <!-- Additional lines to be added here... -->
    <dependencies>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
 </dependencies>
 <build>
     <plugins>
         <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
     </plugins>
 </build>
</project>
Package and Run the App

Once you have created the file, execute the following command to run the code:

mvn package
java -jar target/SpringBootSampleWeb-0.0.1.jar
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com

Recent Posts

Logistic Regression in Machine Learning: Python Example

Last updated: 26th April, 2024 In this blog post, we will discuss the logistic regression…

1 day ago

MSE vs RMSE vs MAE vs MAPE vs R-Squared: When to Use?

Last updated: 22nd April, 2024 As data scientists, we navigate a sea of metrics to…

3 days ago

Gradient Descent in Machine Learning: Python Examples

Last updated: 22nd April, 2024 This post will teach you about the gradient descent algorithm…

6 days ago

Loss Function vs Cost Function vs Objective Function: Examples

Last updated: 19th April, 2024 Among the terminologies used in training machine learning models, the…

1 week ago

Model Parallelism vs Data Parallelism: Examples

Last updated: 19th April, 2024 Model parallelism and data parallelism are two strategies used to…

1 week ago

Model Complexity & Overfitting in Machine Learning: How to Reduce

Last updated: 4th April, 2024 In machine learning, model complexity, and overfitting are related in…

3 weeks ago