Initializing a Spring Boot project with Spring Initializr

Generally, the hardest part of any project is getting started. You need to setup a directory structure for various project artifacts, create a build file, and populate the build file with dependencies. While the Spring Boot CLI removes much of this setup work, if you favor a more traditional Java project structure, then you’ll want to look at the Spring Initializr.

The Spring Initializr is ultimately a web application 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. All you need to do is write the application code.

Spring Initializr can be used several ways, including:

  • Through a web-based interface
  • Via Spring Tool Suite
  • Using the Spring Boot CLI
  • Using Different IDEs

Using Spring Initializr Web Interface

The most straightforward way to use the Spring Initializr is to point your web browser to http://start.spring.io. You should see a form similar the below image.

On the left side of the Initializr form (Project Metadata) you’re asked for some basic information about your project. This includes information such as the project name, the base package name, whether you want to build the project with Maven or Gradle, and which version of Java to build against. If you’re familiar with Maven or Gradle, you probably recognise most of these fields as project metadata that goes into a Maven pom.xml file or a Gradle build.gradle file.

On the right side of the form (Dependencies) you’re asked to specify project dependencies. More accurately, this part of the form is asking you to specify the kind of functionality you intend to develop into your application. For example, if you’re building a web application, you’ll want to select the “Web” checkbox. Likewise, if your application needs security, then you’ll want to select the “Security” checkbox.

 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. The project it generates will be presented to you as a zip file (whose name is determined by the value in the “artifact” field).  The zip file contains a bare-bones project to get you started with developing an application with Spring Boot.

 Creating Maven Example with Spring Boot Initializr Web Interface
  1. Go to Spring Boot Initializr at “http://start.spring.io/”.
  2. Fill the form based on requirements and click on “Generate Project”, it creates and downloads a Maven Project as “<artifact-name>.zip” file into our local file system. Ex: in our example artifact-name is “mySbiWebapp”
  3. Move “mySbiWebapp.zip” to our Spring STS Suite/Eclipse Workspace and Extract this zip file
  4. Import “mySbiWebapp” maven project into IDE (Spring STS Suite or Eclipse etc)

Once you have done the above steps, project structure looks similar to this.

And, If you observe the project files, it generates pom.xml file, Spring Boot Java file and Test Java file. like below.

MySbiWebappApplication.java

MySbiWebappApplicationTests.java

As you can see, there’s very little code in this project. Aside from a couple of empty directories, it also includes the following:

  •  pom.xml—A Maven build specification.
  • MySbiWebappApplication.java—A class with a main() method to bootstrap the application.
  • MySbiWebappApplicationTests.java— An empty JUnit test class instrumented to load a Spring application context using Spring Boot auto-configuration.
  • application.properties— Empty properties file for you to add configuration properties to as you see fit.

Even the  empty directories have  significance in  a  Spring  Boot application.  The static directory is where you can put any  static content  (JavaScript,  stylesheets,  images,  etc)  to be served from the web application. And, as you’ll see later, you can put templates that render model data in the templates directory.

harinathk
 

Click Here to Leave a Comment Below 0 comments