Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Spring Boot “Hello, World”

farenda 2015-06-07 0

Problem:

How to create “Hello, World” app in Spring Boot?

Solution:

Gradle build script will all required dependencies:

buildscript {
    repositories {
        jcenter()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        // Provides tasks to create executable JAR and run project from source.
        // It also adds ResolutionStrategy, which allows to ommit versions in
        // "blessed" dependencies below.
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'

jar {
    baseName = 'spring-boot-hello-world'
    version =  '0.0.1-SNAPSHOT'
}

repositories {
    jcenter()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

Sample REST service to show how it is to do it with Spring Boot:

package com.farenda.solved;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

// Tells Spring to consider this bean for handling incoming web requests
@Controller
// Tells Spring to guess application type based on its dependencies and
// setup application context accordingly:
@EnableAutoConfiguration
public class JavaSolved {

    // Make application accessible under https://localhost:8080/ path:
    @RequestMapping("/")
    // Tells to convert return value into HTTP response:
    @ResponseBody
    String home() {
        return "Hello, Spring Boot World!";
    }

    public static void main(String[] args) throws Exception {
        // Lets Spring bootstrap the application with
        // preconfigured Tomcat. The class name is used
        // to indicate primary Spring component:
        SpringApplication.run(JavaSolved.class, args);
    }
}

Result:

Execute “gradle bootRun” from the console and open https://localhost:8080 to access the app:

$> gradle bootRun
:compileJava
:processResources UP-TO-DATE
:classes
:findMainClass
:bootRun

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.3.RELEASE)

[... a lot of lines ...]
2015-06-06 18:20:19.896  INFO 9273 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2015-06-06 18:20:19.906  INFO 9273 --- [           main] com.farenda.solved.JavaSolved            : Started JavaSolved in 5.255 seconds (JVM running for 6.0)

Let’s use curl to check the result:

$> curl localhost:8080
Hello, Spring Boot World!

It works as advertised!

Share with the World!
Categories Spring Framework Tags java, spring, spring-boot
Previous: Java Switch Case
Next: Java foreach

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored