Welcome back! This tutorial will be a basic introduction to creating a Spring Boot application using IntelliJ IDEA. No prior knowledge is expected, the main purpose of this post is to help anyone new to Spring get rolling quickly writing Spring applications with Spring Boot in IntelliJ. For further reading, be sure to check out Spring’s Guide section for other tutorials on writing Spring Boot applications.
Update 2015-08-26: This tutorial has been updated with the latest version of IntelliJ 15 EAP at the time of writing and describes building an app with IntelliJ’s built in support for Spring Initializr.
Create your new project with IntelliJ
Open up IntelliJ and click on the Create New Project option.
Next, select Spring Initializr from the project type in the left panel, select your Project SDK and then click Next. The Initializr Service URL should already be populated.
Next enter your Maven project properties including name, project type, packaging, Java version, group ID, artifact ID and version for your application. Complete this step by entering a project description and root package. Then click Next.
Next select your Spring Boot version and any Spring Framework dependency your project will require. All of Spring’s projects are available from web, security, cloud, database and so on. Click Next once you’ve selected all your dependencies.
The final step is entering your IntelliJ IDEA project settings. Click Finish when complete.
At this point, IntelliJ may prompt you to add your
pom.xml as a managed Maven project. Click Add as Maven Project to dismiss this prompt and have your Maven dependencies automatically synced with your IntelliJ build path. You’ll see that using Spring Boot, there’s many dependencies you don’t have to add yourself thanks to Spring Boot’s starter POMs.
Exploring the Project
Open up your project’s pom.xml and you’ll see what we just built.
The generated application class
The following class was generated for us and that’s all you need to kick start your Spring Boot application.
This class although very simple looking has a lot going on. First, it’s been annotated with the
@SpringBootApplication annotation. This declares the class as a
@Configuration class for your application. You can define additional
@Bean definitions in it if you like. It adds
@ComponentScan to locate any other beans you define in your project. Finally it adds
@EnableAutoConfiguration in order to enable automatic configuration of your Spring application context by determining what you have available on your classpath.
Create a REST Controller
The next thing you may want to do at this point is add a controller to your application to handle servlet requests.
The first annotation on this controller is
@RequestMapping("/api") and it tells us that this controller will handle all requests beginning with
/api in our application. The second annotation
@RestController tells Spring two things. First this class is a controller bean that should be managed by Spring and will be picked up by component scanning. Secondly, all methods inherently use
@ResponseBody semantics which means the return value will become the body of our servlet response.
I’ve only defined a single method in this controller responding to the
GET HTTP method, and it produces JSON output. My method merely returns a new instance of the Widget class. Your controller will return something else that’s related to your business case.
Running your Application
At this point, you can run your application simply by right clicking inside your application class and selecting
Run SpringBootHelloWorldApplication.main(). Then browse to
http://localhost:8080/api and you’ll see the response from the controller and request mapping we just created.
By now, you should know how to create a new Spring Boot application from scratch using IntelliJ, it’s really quite simple with the introduction of Spring Initializr built into the IDE. I’ve also provided a very brief introduction to writing a Spring Boot app to get you started.
If you’d like to view the source code of this application, it’s available on Github https://github.com/pgrimard/spring-boot-hello-world.