Learn how to create the infrastructure for a Spring Boot project with Spring Initializr UI.
- [Instructor] So let's create our first Spring Boot project with Spring Initializr. First, we open a browser and navigate to https://start.spring.io We're going to select a Maven project. You can choose to use Gradle if you're more comfortable with it, but in this tutorial we will use Maven. Use the latest version of Spring Boot. And the group will be called com.example.ec for Explore California, the artifact will be explorecali.
And we need more options, so I'm going to switch to full version. The name is explorecali, we're going to rename the description to Explore California Microservice. The package name will be the default. We're going to be packaging as a Jar, not a War. Java version 8, and of course the language is Java. Now, we're going to scroll past the Generate Project button and look at all of these Spring Starter packages, and from these we're going to choose Web and within Web is Rest Repositories.
And then keep scrolling, and we get to the SQL part, we're going to choose JPA and H2. Now we're going to go back and click the Generate Project button. Now Spring Initializr will generate the zip file. I will copy it to my working folder and unzip the file there. So the folder is in my exercise file in Chapter 1, Finished, explorecali is where I unzipped it to and these are some of the files that are generated and the src folder.
Now I'm going to open it in IntelliJ. Open, select the folder I want, Desktop, Chapter 1, Finished, explorecali, that's the main folder but I must select pom.xml to tell IntelliJ that it is a Maven project. Click OK. Delete existing, import this one. And I'm going to hit cmd + "1" to get the project view.
Let's see what Spring Initializr created for you. You have the directory structure that has the source folder, a Maven pom.xml, configuration files, some Maven helper files, and in the src, main, Java, com.example.ec there is an ExplorecaliApplication Java class. Let's open the pom.xml. Inside the pom file, there is a Spring Boot Starter parent, and all of the Spring Boot Starter dependencies that we selected on the Initializr webpage.
Now let's look at the ExplorecaliApplication.java file. Open that. It's not a very large file, but two things jump out at us. This Java class has a main method and there is an @SpringBootApplication annotation. If you have been writing Java web applications for years, it's probably been a long time since you have written a class with a main method. This is a major paradigm shift.
We get the main control back. We are no longer writing code that has to be packaged into a War file and deployed on a web or application server. The web server is deployed in our application. Adding the @SpringBootApplication annotation to this class with the main method tells Java here is where our Spring Boot Microservice starts. Command line parameters or special startup logic resides here.
With this one pom file, and one Java source file, we can now build and run the project as a Spring Boot application.
- Setting up the project
- Building, deploying, and launch the microservice
- Declaring Spring Data JPA repository interfaces
- Invoking repositories
- Using Spring Data query methods
- Exposing RESTful APIs with Spring Data REST
- Using the /search resource to invoke query methods
- Paging and sorting
- Declaring a new REST controller
- Creating HTTP methods for creating, reading, updating and deleting persistent data.