Spring Boot Initializr Web Interface and Examples

Hello friends lets discuss another important components Spring Boot Initializr of Spring Boot, it is a quick way to create spring boot project structure. In this chapter we are going to explore about a web interface which create Spring Boot Application online.

Related tutorials previously we have discussed

What is Spring Boot Initializr?
The Spring Initializr is ultimately a web application (at “http://start.spring.io/”) that can generate a Spring Boot project structure for you. It doesn’t generate any application code, but it will give you a basic project structure and either a Maven or a Gradle build specification to build your code with.

Spring Initializr can be used in several ways:
  • Through a web-based interface
  • Via Spring Tool Suite
  • Via IntelliJ IDEA
  • Using the Spring Boot CLI
Why we need Spring Boot Initializr?
Sometimes the hardest part of a project is getting started. You need to set up a directory structure for various project artifacts, create a build file, and populate the build file with dependencies. The Spring Boot CLI removes much of this setup work, but if you favor a more traditional Java project structure, you’ll want to look at the Spring Initializr.

USING SPRING INITIALIZR’S WEB INTERFACE
Now we are starting with the web-based interface.

The Spring Team has provided a Web Interface for Spring Boot Initializr at “http://start.spring.io/”. We can use it to create our new Project’s base structure for Maven/Gradle build tools.


Creating Maven Example with Spring Boot Initializr Web Interface
There are following steps to create new Spring Boot Web Application for Maven Build tool and Spring STS Suite IDE.

Step 1: Go to Spring Boot Initializr at “http://start.spring.io/”.

Step 2: Once you’ve filled in the form and made your dependency selections, click the Generate Project button to have Spring Initializr generate a project for you.


Step 3: Now click on “Generate Project” Button, it creates and downloads a Maven Project as “myapp.zip” file into our local file system.

Step 4: Move “myapp.zip” to our Spring STS Suite Workspace and Extract this zip file


Step 5: Import this “myapp” Maven project into Spring STS IDE.

Step 6: We’d have a project structure similar.


If you observe this project files, it generates pom.xml file, two Spring Boot Java files and one JUnit Java file.
<?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>com.dineshonjava</groupId>
 <artifactId>myapp</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>myapp</name>
 <description>Demo project for Spring Boot</description>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.3.5.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>
 
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
 

</project>

MyappApplication.java

package com.dineshonjava;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyappApplication {

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


MyappApplicationTests.java
package com.dineshonjava;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyappApplication.class)
@WebAppConfiguration
public class MyappApplicationTests {

 @Test
 public void contextLoads() {
 }

}

Creating Gradle Example with Spring Boot Initializr Web Interface
All steps for creating gradle project with Spring Boot Initializr is same as like Maven Project as we have created above unlike select Gradle Project instead of Maven Project in Spring Boot Initializr Web Interface.


All source code files name and structure of project is also same as like maven project unlike creating build file name build.gradle instead of pom.xml


build.gradle
buildscript {
 ext {
  springBootVersion = '1.3.5.RELEASE'
 }
 repositories {
  mavenCentral()
 }
 dependencies {
  classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
 }
}

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

jar {
 baseName = 'mygradleapp'
 version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
 mavenCentral()
}


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


eclipse {
 classpath {
   containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
   containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
 }
}


As you can see, there’s very little code in this project. Aside from a couple of empty directories, it also includes the following:
  • build.gradle—A Gradle build specification. Had you chosen a Maven project, this would be replaced with pom.xml.
  • Application.java—A class with a main() method to bootstrap the application.
  • ApplicationTests.java— an empty JUnit test class instrumented to load a Spring application context using Spring Boot auto-configuration.
  • application.properties—an empty properties file for you to add configuration properties to as you see fit.


Summary
Whether you use Initializr’s web-based interface, create your projects from Spring Tool Suite, or use the Spring Boot CLI to initialize a project, projects created using the Spring Boot Initializr have a familiar project layout, not unlike other Java projects you may have developed before.

Spring Boot is an exciting new way to develop Spring applications with minimal friction from the framework itself. Auto-configuration eliminates much of the boilerplate configuration that infests traditional Spring applications. Spring Boot starters enable you to specify build dependencies by what they offer rather than use explicit library names and version. The Spring Boot CLI takes Spring Boot’s frictionless development model to a whole new level by enabling quick and easy development with Groovy from the command line. And the Actuator lets you look inside your running application to see what and how Spring Boot has done.


Happy Spring Boot Learning!!!


No comments:

Post a Comment