Using the Spring Initializr
Hey hackers, welcome back. Once you learn Java, the next best thing to learn is Java Spring Boot. Java Spring and Java Spring Boot allow you to create Java applications pretty easily. To get started with Spring Boot, we'll use the Spring Boot Initializr.
The Spring Initializr gives us a starter package that we can use to create our first Spring application. We'll use Maven as our Java package manager. This is similar to PIP for Python or NPM for Node. Our language is Java, so we'll keep that the same. The Spring Boot version isn't super important here because we'll be doing the basics, so we'll keep the default. Then we'll set up the metadata for our application. I'm going to add Blondie Bytes and rename this to Tutorial. We'll package our application as a JAR, and we're going to use the latest version of Java, so that's Java 15. If you want to use this version of Java, make sure you have the JDK for Java 15 installed. That can be done by simply Googling Java JDK, and you can download it here.
Now for the fun, we'll add the dependencies. This is all the code we get for free. To keep things simple, we're only going to include Spring Web for now, but we could use any of these dependencies, and this would enhance our application even more. Let's click generate, and a sample project is downloaded.
Importing the Project into IntelliJ
We use the Spring Initializr because it helps us get some of the boilerplate code out of the way so we can focus on our application code. We'll open it up and drag it into the desktop. To edit our sample code, I'm going to use IntelliJ, which is my IDE, but you could use any IDE you like: Eclipse, NetBeans, whatever. IDE you use, you'll have to install it. So if you want to use IntelliJ, make sure you install it from the internet. IDEs just make it easier for us to write our application code.
We're going to import our tutorial project that's right here in our desktop, and we're going to import the project using Maven, that package manager. When we import with Maven, it'll pull down the dependencies for us. We're going to select 'import Maven projects automatically' and we'll select the JDK 15. This matches up with Java 15, which is the language version that we're using. If you selected Java 15 in the Spring Initializr, then you'll want to download the JDK and select it here as well. They have to match in order for your program to work. Let's click Next. We'll click Next. The JDK 15 is selected, so this looks good. The project's on my desktop. We'll call it Tutorial, and let's load it up.
Exploring the Project Structure
All right, let's take a look. Here we have our source code, main, Java, our project. This tutorial application, it has our base main function that runs the Spring application. We have an application.properties file for configuration. We have a folder for our test classes, so our unit tests and integration tests. We have a pom file that specifies the Java version. If you're getting any errors, make sure that's set here. It also specifies our dependencies, and so that's Spring Boot Starter Test, Spring Boot Starter Web, and a Maven plugin. Spring Boot Starter Web is actually what we selected previously in the dependencies window. The parent of this application is Spring Boot Starter Parent. This gives us a bunch of stuff by default.
Troubleshooting JDK Version Mismatches
If you're having any issues with the loading here, there are a couple things you can do. Likely it's due to mismatched JDK versions. If you go to External Libraries, make sure this is 15, or if you're not using Java 15, make sure all of these match. So in the properties, java.version it says 15. If I select on my project and go to Module Settings, we'll select SDK. Here it says 15. All of this looks great. And if you need to reset your path for your Java version, I have this nifty function in my aliases where I can just type in jdk 15, and it will switch to the appropriate JDK type. If you want to configure this, I'll show it in my bash profile. Here you can see it: jdk() it's a function. It takes in a Java version, and then it resets where my JAVA_HOME variable looks at. In this case, it's going to look at that version, and then it prints out the new version for us, in this case, Java 15.
This can be something useful. You could also just do something like this right in the terminal, and you can put in that version number right here. And that will do the exact same thing. It won't print out the version because you'd also need to add this command. And everything looks great. Let's clear this up.
Running the Application for the First Time
And we'll run our application. To run it, I'm just going to select TutorialApplication, click Run, and this will start it up. We see at the top it's using JDK 15 and it looks like we have something on this port. If you're on a Mac, you can actually open up the Activity Monitor, search for Java, and cancel these programs so that you can run your application. It looks like a previous app was running before. Let's run our app again. Since we've run it before, it shows up here in IntelliJ so I can just click Play. And it started up here. Tomcat, which is a Tomcat server, that's what Spring is built on. By default, we're starting it at port 8080. Now we need to add some code to make our app do anything. So let's stop it for now and add some code.
Creating and Testing Your First Endpoint
We're going to create a simple web application. All we want to do is display a message in our browser at localhost:8080. To do this, we'll need to create a REST controller. It's going to live in its own class. We'll call it GreetingController. And we'll give it the annotation @RestController. The REST controller will be a component that contains routes of our application and what happens when users visit those routes. To set up a route, we'll create a function called getGreeting and we'll have it return a message that we want displayed on our localhost:8080 page. Some people do a basic 'Hello World' message, but we're not basic, so we're going to say, 'Hi, it's me Blondie Bytes, and I like pizza.' I like to create a custom hello world message to make sure it's me and it's not some default thing coming from somewhere else in the application. We are actually not returning void, we're returning a string. And now our application looks good.
This is just a function, but to make it a route in our application, we'll need to add the @RequestMapping annotation. So we'll just type RequestMapping. These are annotations that are built into the Spring framework. See these import statements up here? If we did not have those dependencies in our pom file, then we wouldn't be able to use this. Now, we're not done with this RequestMapping. We have a bunch of different values we can give it. We can give it a name, value, path, method. The path we're just going to give it the backslash. And we could also write something like path = but we don't need to do this, we can just do the slash. The default method is a GET request, so we don't need to add that here either. If we wanted to make this a PUT or a POST, we could just add method = POST or whatever method we'd like to do here. Since we want to do a GET, it's the default, so we don't need to add that. So with the RestController and with the RequestMapping annotation, when I run the app and go to the localhost home page, I should see our greeting. Let's run it. Let's see what happens.
All right, our application has started on port 8080. So if I open my browser here, go to localhost:8080, there's our message, 'Hi, it's me BlondieBytes and I like pizza.' With this application, we have a running REST API with one GET endpoint: the home endpoint or the home route.
Modifying Endpoints and Refactoring
If we made this /greeting, stopped our application and ran it again, then we would see this display message at localhost:8080/greeting. Let's try it. All right, it started up. We're going to go to the home route. We get an error. This is good because our route is now located at /greeting. And there's our greeting. You can have multiple controllers in your application with related routes put together. So I like to put all the controllers in their own package. To do that, I'll go new package, controller, and we'll put our GreetingController in there. This helps keep our code a little more organized.
Understanding Core Spring Concepts
What we've created here is a Spring Boot application. When people say they're using Spring or Spring Boot, there's so many different things they could be talking about. Remember that long list of dependencies? It's kind of like saying you're using Amazon Web Services. It's so generic that you really need to specify what services you're familiar with.
Here we are using Spring MVC: Model View Controller. Our model is the return statement, but we could have created model classes to organize and process the data we return. Our controller is the GreetingController, which maps the greeting statement to a given route, in this case /greeting. The @RestController annotation actually does two things for us. It combines the @Controller and @ResponseBody annotations. This allows us to just return the string data rather than a full view itself. It allows us to easily create a REST API. When people say they use Spring Boot, they often mean they use the @SpringBootApplication annotation in their main class to set up a bunch of stuff behind the scenes. This makes their application easier to create and maintain. Otherwise, there would be more code we'd have to write. So if you want to create a quick REST API microservice using Java, Spring Boot with Spring MVC is a pretty good bet.
Conclusion and Next Steps
I've recently gotten into Spring, and there's so many more concepts than we've discussed here, but this should get you started with the foundation. If you're interested in learning more Spring concepts such as dependency injection and component scanning, Java Beans, or even what some of the annotations are, just leave a comment down below because the next step after learning Java is to learn Java Spring and Java Spring Boot. Happy coding.