Monday, November 23, 2015

Hello World using Spring boot and Spring 4.x


Buzz word Spring boot.

Although now Spring boot is not a buzz word any more still from experimenting purpose I am targeting hello world program via Spring boot in this blog.  Here I will tell you how simple it is to generate json via Spring boot for an entity.  I am not using database operation for the sake of simplicity.

Technology stack

  • Java8
  • Spring boot 1.3.x
  • Spring 4.2.3
  • Apache Maven 3.3.x
  • IDE - IntelliJ 14.x Community Edition

Just write few lines of code and you are done in generating Json.  How?  Let's see.

I just added pom.xml, one controller, one model (optional) and which will be starting point for Spring boot.  Since explanation wise there is not much I am directly coming to the code.

File pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""







package com.spring.experiments.controller;

import com.spring.experiments.model.Notebook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;

 * Created by shaiverm on 19-Nov-2015
public class HelloController {

    public String index() {
        return "Greetings from Spring Boot!";

    public Notebook getNotebook () {
        Notebook notebook = new Notebook();
        notebook.setCreatedOn(new Date(new java.util.Date().getTime()));
        notebook.setDescription("New features from Java8");
        return notebook;


package com.spring.experiments.model;

import javax.persistence.*;
import java.sql.Date;

 * Created by shaiverm on 17-Nov-2015
public class Notebook {

    private Long id;

    private String name, description, tags, author;

    private Date createdOn;

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getDescription() {
        return description;

    public void setDescription(String description) {
        this.description = description;

    public String getTags() {
        return tags;

    public void setTags(String tags) {
        this.tags = tags;

    public String getAuthor() {
        return author;

    public void setAuthor(String author) { = author;

    public Date getCreatedOn() {
        return createdOn;

    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;

    public String
    toString() {
        return "Notebook{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                ", tags='" + tags + '\'' +
                ", author='" + author + '\'' +
                ", createdOn=" + createdOn +


package com.spring.experiments;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

public class Application {

    public static void main(String[] args) {
        System.out.println("Spring Boot begins...");
        ApplicationContext ctx =, args);

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        for (String beanName : beanNames) {
        System.out.println("Spring Boot started...");


Output Sample
Hit Url - http://localhost:8080/notebook/java from a browser (I did this on chrome) and json will be in front of you.

How to Run

  • mvn spring-boot:run
  • Use IDE by running
  • Make this application as webapp and deploy in Tomcat/Jetty or any web server.

Code at Github
I have posted my same code at Github.  Please visit below link to access it.

You can reach me for any queries at my email address.