
Lesson Description
The "Spring Boot Setup & Code Formatting" Lesson is part of the full, Enterprise Java with Spring Boot course featured in this preview video. Here's what you'd learn in this lesson:
Josh revisits start.spring.io and explains and highlights the ergonomics of creating a new project. Many IDEs, such as IntelliJ, VS Code, and Eclipse, also rely on start.spring.io. The spring-javaformat code formatter is also discussed.
Transcript from the "Spring Boot Setup & Code Formatting" Lesson
[00:00:01]
>> Josh Long: We used Start Spring IO. This is another thing that's worth understanding is that Start Spring IO is where I'm going to generate all my new projects today. I'm going to go here, Start Spring IO and we're going to generate new projects. You can choose all these different things.
[00:00:14]
We saw an enumeration of that when you use your ide. If you go to Intellij for example, and you go to File new project and then you choose Spring boot, this is in turning, in turn using Start Spring IO. So the integration in your IDE is just talking to the REST API powered by Start Spring IO.
[00:00:37]
And then that's a good point. Start Spring IO is a REST API, it's a service. So if I curl Start Spring IO, you get a menu of all the different dependencies that are available here, there you go. It's a table, believe it or not. You just can't really see because it's weird.
[00:00:54]
So go over here. There you go. Start that Spring.io, here's the web service. You just curl it, right? So you can initialize, you can automate all these different projects that we're going to build. And in fact, if you go here and you get a configuration of a project you really like, let's say I want to make sure that every using Java 24 and Maven and 3.4 and the web support, just share it, right?
[00:01:16]
Share. So that is your endpoint to generate that project. Go there and it'll actually generate the same exact project, identically configured. Or you can script it, like I said, you can do one of these. You can take curl start.spring.io and add web,data-jpa, specify gradle, specify base directory, etc.
[00:01:40]
So this is a very, very useful tool to incept new projects. I use the browser based experience because I'm a noob. But you can use whatever you want, right? If you wanna do the CLI and that makes you happy, go for it, it's there. It's the same thing that the IDEs use to win.
[00:01:57]
What about IDEs? What are you using for your editor?
>> Speaker 2: IntelliJ.
>> Josh Long: Okay. Same. Same, IntelliJ? That's probably gonna be in the norm, but it doesn't have to be. I love intellij, I'm using intellij. You don't have to though, that's all I'm trying to say. So in the Java ecosystem there's a.
[00:02:17]
It used to be that Eclipse was the thing, right? Eclipse was the IDE of choice. And so when I started in my Role as a spring developer Advocate. Back in 2010, I would ask people, I'd poll the audience, how many people are using Eclipse, how many using IntelliJ?
[00:02:30]
The minority were using IntelliJ and the very vast majority was using Eclipse. That said, the tables have sort of turned at this point. Yes sir.
>> Speaker 3: People online were saying IntelliJ, VS Code and.
>> Josh Long: Dope, so good. Okay, so we'll talk about those in a second. Things have definitely changed.
[00:02:49]
IntelliJ is now the majority. That said, there's a new contender in town, isn't there, right? Which he just alluded to, which is Visual Studio code. When I say new, I mean more than a decade at this point. But one of the things that's so interesting about Visual Studio code is that it has this thing called a language server model.
[00:03:05]
And basically Microsoft, faced with trying to make inroads in existing, well entrenched industries and ecosystems like Java, was wondering how they could bring the best of that ecosystem into an IDE built in node. And they realized, okay, well we're not going to be able to rebuild the best Java tooling in a language that's not Java.
[00:03:27]
Ditto for net, ditto for Python, ditto for every other language. So they built a component model, they built a plugin model based on a protocol. It's a network, it's a wire protocol. It's not a wire protocol. It's a in process, separate process protocol. It's called Language Server LSP Language Server Protocol.
[00:03:44]
And that protocol, super good, right? It's taken off. There's been a Cambrian explosion. That's why Visual Studio code is so darn popular. And that's why you can run and write code so effectively in one tool against a number of different languages. So we who have we on the spring team, we built this tooling called the Spring Tool Suite.
[00:04:02]
It's a set of plug-ins for Eclipse that was very, very popular. Obviously these days I would expect that IntelliJ is more popular that plugin. That support though was still very good. We extracted that from Eclipse and packaged it as an lsp. So now when you use Visual Studio code and you install the spring boot support, you're using the support that we built for Eclipse.
[00:04:23]
It's as good as anything out there, right? It's very, very good. So the most popular way to build spring boot tools is to use the, what is it called, the Java bundle or whatever in Visual Studio code that installs the JVM support, the JVM Java LSP and then it installs the spring boot support, which is from us.
[00:04:40]
So we are the maintainers of that LSP that's also very recommended and same thing in those experiences. Inside of Eclipse or inside of Visual Studio code, there is a wizard that you can use to go to start spring IO. And guess what? All it does is it talks to the backend rest API that I just showed you.
[00:04:55]
So all roads lead to start.spring.io, okay? Command line, the browser, your IntelliJ, your Eclipse, your Visual Studio Code. Somebody mentioned neovim. You can use LSPS from neovim. Don't ask me how I actually got LSPS working in Emacs and that felt satisfying enough. I don't know about neovim. I wish prime were here, he'd sort us out, but okay.
[00:05:21]
Anyway, use whatever you want. It's my point. I don't actually care. And the reason I don't care is because you should build your project in such a way that it doesn't matter what tools you yourself are working on, right? Again, git clone run. So we talked about start.spring.io and how you can use it to incept a new project.
[00:05:39]
I actually just did that. I have a project here, Start Spring that I built and I wrote some code. Not a lot of code. This is 1, 2, 3, 4, 5, 6, this is 7 and technically 8. But do we really. Is that fair? Couldn't it just be seven?
[00:05:56]
And does this have to be there? See, that's still fine. Right? Whatever. And my point is, it's not even 10 lines of code. Even if you have the extra new lines for the one curly bracket, it's a very, very small amount of code. But imagine something more significant, more meaty, right?
[00:06:11]
And imagine people contributing in every other IDE I'm using IntelliJ, if I do this, let's say I just commit the code like that into the. Into the git repository. Is that gonna make me friends? This is not how we should behave. Now, if we're all using IntelliJ, maybe we can just have a pre commit that just does the IntelliJ code formatting.
[00:06:32]
But I quite like having a little bit of enforcement whenever I can. So one of my favorite tools is the Spring Java format Maven plugin or Gradle. It's available for Gradle. The Spring boot team built this. We actually built this plugin ourselves. And it's called, there you go, spring-javaformat, GitHub.com, spring-io spring-javaformat, okay?
[00:06:57]
Okay, and I'll just go ahead and copy and paste this Stanza. Here, take that. Put that in my pom xml. Go down to my plugins paste. Get rid of the extra duplicative plugins stands elements. Fantastic. Now if I go back to my source code, lose friends. Get rid of that.
[00:07:23]
Now I do maven package. Let's say I do a package. I'm going to build the code. In effect, it says, hey, formatting violations found. Okay, fine. How do I fix it? Maven do that, oops, don't wanna subshell. Okay, there you go. So now it's formatted the code. Okay, so the code looks exactly the same.
[00:07:47]
I'm going to say something and I'll never repeat it, but I will say it. This is one of two things that go got right. There, I've said it. Surely nobody heard that though. This is one of two things that got right. Go format is awesome. It's good. It's a very good idea, right?
[00:08:04]
It ensures peace in the office space, right? Like this is such a good idea. Because it doesn't matter if you're using neovim or Visual Studio code, or Intellij or Eclipse or whatever. There's only one true formatting. The build will break if you don't adhere to it. And it's automated, so just why not, right, who cares?
[00:08:20]
You can use whatever local thing you want. There's no arguments about tab versus space. None of that just goes out the window. You can focus on the things that matter. Now look at this. This is kind of ugly. Look what just happened to my code. Though. Clearly this is not meant to be written in such a way that they're all kind of like a train car crash collision thing here.
[00:08:38]
So I hit Enter, //, right, and then over here. Now go back, reformat again. There you go. That's better. So the end of line format character helps it decide to move things to the next line and does a pretty good job given that advice. If you need things to be on the next line, use at the end of the line.
[00:09:00]
Okay, So I love that Spring Java format plugin. You notice that I've got it set up to bark if the code doesn't have correct Java format. That's very, very useful because again, you want people to validate this before they push. So it's validate, etc.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops