Monday, November 23, 2015

Hello World using Spring boot and Spring 4.x

Hi,

Buzz word Spring boot.

Problem
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


Solution
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 Application.java 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="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>spring</groupId>
    <artifactId>boot</artifactId>
    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <start-class>com.spring.experiments.Application</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>


File HelloController.java
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
 */
@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }

    @RequestMapping("/notebook/java")
    public Notebook getNotebook () {
        Notebook notebook = new Notebook();
        notebook.setAuthor("Vermaji");
        notebook.setCreatedOn(new Date(new java.util.Date().getTime()));
        notebook.setId(1L);
        notebook.setName("Java8");
        notebook.setDescription("New features from Java8");
        return notebook;
    }


}


File Notebook.java
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) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.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) {
        this.author = author;
    }

    public Date getCreatedOn() {
        return createdOn;
    }

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

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

}


File Application.java
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;

@SpringBootApplication
public class Application {

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

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

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
        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
              or
  • Use IDE by running Application.java
              or
  • 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.

Thanks
Shailendra