Over a million developers have joined DZone.

Spring(boot)ing to the Web With Java and Pom.xml

Following along as one developer shows you how to set up an app with Spring Boot using some Java and a single Pom.xml file.

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

This blog presents instructions on how to quickly get started with Spring Boot Hello World with just one Java file and a Pom.xml.

Image title

Before getting to the files, make sure you have installed and configured the following:

  • Java: Set up Java_Home, as it is required by Maven.
  • Maven: Maven would be used to package our app.

Java Code Sample

Below is the code for the 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);
    }

}

 

Pom.xml Code Sample

The following is the code for the Pom.xml file. Make sure to put Pom.xml at the topmost level (alongside the 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 

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:
java ,spring boot

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}