A quick introduction into the CLI and how a simple tweet showed the world the power of Spring Boot.
- [Narrator] I like to think that all good things have a point in time that really defines it. To me, this tweet represents that point in time for Spring Boot. In August 2013, prior to the SpringOne conference that year, Rob Winch, the Spring Security Lead, and my fellow co-funder for the Kansas City Spring User Group and the Kansas City Java User Group, tweeted this out to the world. Less than 140 characters, a fully-functioning Groovy code, executed a web service.
Now granted, it's just a "Hello, World!" Application, but that's where all programs ultimately get their start. And this was really the start for Spring Boot. In all reality, there was a community-submitted ticket that led to the Spring Boot project as a whole, but this tweet is part of the announcement. So, I want to take a moment to prove to you, that indeed, this tweet executes a fully-functioning web service. And to do that, I'm going to jump into the terminal.
Now I'm going to use vi, just to create this file. And though it's Groovy, and you may not know Groovy very well, none of that really matters. What matters is that we're going to actually prove to you, that this tweet contains enough information with the Spring Boot CLI, to actually execute a web service. Furthermore, as we go through the contents of this tweet, I want you to note that in today's world, this could be done even more efficiently.
But again, we're just going to go with the contents of that tweet that Rob posted out in August 2013. So we'll go ahead and create our Groovy class. And in that Groovy class, we're going to annotate a method, with RequestMapping at the root... and ResponseBody. And we're going to create a method now, that returns a String, and we'll just call it home. And we're going to return the string "Hello World." Now, you may notice some similarities here to Java, and after all, Groovy will compile down to traditional JVM bytecode.
But, for now, that's all that matters. Let's go ahead and save this file out and you'll see that I have a file called, ThisWillActuallyRun.groovy and I'm going to execute spring run and then this file name. Now, Spring's going to start up and do a whole bunch of work. And we'll talk about what's going on now, but essentially once it's done, and you can tell it's done when we see the command JVM running, and it tells you how long it took to start up. You'll notice right above that in the log message, that there is an execution of Tomcat running on port 8080.
So let's jump over to Chrome, and we'll navigate to localhost:8080, and indeed, you'll see I have Hello World being responded to. I'm going to kill this application. And once the application itself has shut down, we'll go back over to the browser and refresh. And you'll see now, as expected, the site cannot be reached. So how does all of this work? As I briefly mentioned, you've got to have the Spring Boot CLI installed.
And the Spring Boot CLI, with some Groovy code, will execute a Spring Boot application and everything that it needs to run. We save that Groovy file and we ran a command, $ spring run, and then the file name, in this case, ThisWillActuallyRun, from the command line. So what really happened? First thing to note, and I alluded to it a moment ago, is that Groovy, indeed, compiles to JVM bytecode. The Spring Boot CLI along with the application file that we created, resolved all of the needed dependencies.
Which, for the most part, consisted of the Spring Boot starter's auto-configuration and web, and everything that comes with them. It created a container for running the code. An application context, and if you look through all the log messages, you will see that application context being created in the log messages. As well as all the auto-configuration that we're going to talk about in a little bit. Finally, it started up an embedded Tomcat instance on port 8080, and it served the service through the controller mapping and all of the request mappings that go with that.
All of that, in less than 140 characters, which at the time was the maximum length of a Twitter tweet, and you have a fully-functioning web service. That was the power of Spring Boot when it was announced to the world.
- Creating a Spring Boot application
- Configuring a Spring Boot app
- Leveraging profiles
- Packaging and running Spring Boot web apps
- Building a command-line application
- Using Spring Boot starters: Spring Data, Spring Security, and more
- Extending Spring Boot
- Using Spring Boot Actuator to monitor app health and other metrics